首页 > 学院 > 开发设计 > 正文

[2017-AspNet-MVC4] 简单加法的演化-1

2019-11-06 06:51:03
字体:
来源:转载
供稿:网友

目标:

完成一个简单的整数加法

过程:

1.新建一个Basic类型的MVC4的PRoject

2.新建一个Home Controller,代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace AddTest.Controllers{    public class HomeController : Controller    {           public ActionResult Index()        {            return View();        }        public ActionResult CalSum(string FirstNum,string SecondNum)        {            int a, b, c;            a = int.Parse(FirstNum);            b = int.Parse(SecondNum);            c = a + b;            ViewBag.vc = c.ToString();            return View("Index");        }    }}3.为Index Action新建一个相应的View,代码如下:

@{    ViewBag.Title = "AddTest";}<h2>AddTest</h2>@using (Html.BeginForm("CalSum", "Home")){       @Html.TextBox("FirstNum") <b>+</b>    @Html.TextBox("SecondNum") <b>=</b>    @Html.TextBox("SumNum",(string)ViewBag.vc)    <input type="submit"/>}

小结:

数据从Action发往View时,采用视图包裹(ViewBag).字符串转换为整数采用:int.Parse函数.待解决问题:如果输入小数或其它字符,提交后,界面为系统错误.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表