使用Html.BeginForm来提交表单

https://www.cnblogs.com/m2345/p/5361529.html

 

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"] %>
原文地址:https://www.cnblogs.com/wfy680/p/14280603.html