目标:
完成一个简单的整数加法
过程:
1.新建一个Basic类型的MVC4的Project
2.新建一个Home Controller,代码如下:
复制代码
3.为Index Action新建一个相应的View,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28using 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"); } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15@{ 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函数.待解决问题:如果输入小数或其它字符,提交后,界面为系统错误.
最后
以上就是干净外套最近收集整理的关于[2017-AspNet-MVC4] 简单加法的演化-1-整数加法的全部内容,更多相关[2017-AspNet-MVC4]内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复