Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步的表单提交,这对于我们开发者来说是一个福音,我们不用再自己去用JQ代码了,直接用MVC自代的Ajax.BeginForm就可以很容易的完成一个异步的表单提交动作。
Html.BeginForm的原型解释:
一、
@using (Html.BeginForm()) {} //提交到当前页面
@using (Html.BeginForm()) { <p>Your name:@Html.TextBoxFor(x=>x.Name)</p> <p>Your email:@Html.TextBoxFor(x => x.Email)</p> <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="Yes.I'll be there",Value=bool.TrueString },new SelectListItem(){ Text="No,I can't cpme",Value=bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP"/> }
二、
@using (Html.BeginForm(new {} )) {} //提交到当前页面,并可以传递参数
@using (Html.BeginForm(new {Name="yhf2014" })) { @* <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>*@ <p>Your email:@Html.TextBoxFor(x => x.Email)</p> <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="Yes.I'll be there",Value=bool.TrueString },new SelectListItem(){ Text="No,I can't cpme",Value=bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP"/> }三、
@using (Html.BeginForm("action","controller")) {} //提交到指定controller下的action中
@using (Html.BeginForm("RsvpForm", "Home")) { <p>Your name:@Html.TextBoxFor(x=>x.Name)</p> <p>Your email:@Html.TextBoxFor(x => x.Email)</p> <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="Yes.I'll be there",Value=bool.TrueString },new SelectListItem(){ Text="No,I can't cpme",Value=bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP"/> }
四、
@using (Html.BeginForm("action","controller",FormMethod.POST)) {} //提交到指定controller下的action中,并指定提交方式
@using (Html.BeginForm("RsvpForm", "Home", FormMethod.Post)) { <p>Your name:@Html.TextBoxFor(x=>x.Name)</p> <p>Your email:@Html.TextBoxFor(x => x.Email)</p> <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="Yes.I'll be there",Value=bool.TrueString },new SelectListItem(){ Text="No,I can't cpme",Value=bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP"/> }
五、
@using (Html.BeginForm("action","controller",FormMethod.POST,bject htmlAttributes)) {} //提交到指定controller下的action中,并指定提交方式,并给form一个属性。
@using (Html.BeginForm("RsvpForm", "Home", FormMethod.Post, new {name="rsvpForm1" })) { @* <p>Your name:@Html.TextBoxFor(x=>x.Name)</p>*@ <p>Your email:@Html.TextBoxFor(x => x.Email)</p> <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="Yes.I'll be there",Value=bool.TrueString },new SelectListItem(){ Text="No,I can't cpme",Value=bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP"/> }