启动VS2010平台,新建VC# MVC4 Project,Basic类型
整体构成及Index.cshtml内容:
AddCal.cshtml Code:
@{
ViewBag.Title = "AddCal";
}
<h2>AddCal</h2>
@using(Html.BeginForm("AddCal","Home"))
{
@Html.TextBox("FirstNum") <text>+</text>
@Html.TextBox("SecondNum")<text>=</text>
@Html.TextBox("SumNum",(string)ViewBag.SumNum)
<br />
<input type="submit" />
}
HomeController.cs Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcAddTest.Controllers
{
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult AddCal()
{
ViewBag.SumNum = "";
return View();
}
[HttpPost]
public ActionResult AddCal(string FirstNum,string SecondNum)
{
int a, b,c;
a = int.Parse(FirstNum);
b = int.Parse(SecondNum);
c = a + b;
ViewBag.SumNum = c.ToString();
return View();
}
}
}
小结:了解提交方式Get及Post的区别.了解前端View与后台Action之间的呼应关系.了解ViewBag传递数据的方法.了解字符串和整数的转换方法.了解Html助记符的写法.