1.Html.BeginForm()
該方法用於構建一個From表單的開始,他的構造方法為:
Html.BeginForm("ActionName","ControllerName",FormMethod.method)
一般構建一個表單結構如下
<% using(Html.BeginForm ("index","home",FormMethod.Post)){ %> 。。。。。。 <%} %>
他將在客戶端產生一個類似<form action="/account/login"
method="post"></form>標簽
2.現在開始創建一個表單實例,首先在Index.aspx中構建一個表單
<% using(Html.BeginForm ("index","home",FormMethod.Post)){ %> 帳號:<%=Html .TextBox ("username") %> <br/> 密碼:<%=Html .Password ("password") %> <br /> <input type="submit" value="登錄" /> <%} %>
3.在對應得控制器HomeController.cs中寫入下面代碼,傳遞出一個ViewData[]字典:
public ActionResult Index() { string struser = Request.Form["username"]; string strpass = Request.Form["password"]; ViewData["w"] = "你的賬號是:" + struser + "你的密碼是:" + strpass; return View(); }
4.在Index.aspx中寫接受傳值
<%=ViewData ["w"] %>