当前位置: 代码迷 >> 综合 >> 构件Form表单
  详细解决方案

构件Form表单

热度:88   发布时间:2024-01-21 23:02:52.0
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"] %>

 

     5。运行效果如下

 

  相关解决方案