下面是总结一下在ASP.NET MVC中表单的几种提交方式。
1.Ajax提交表单
需要引用
<script type="text/javascript" src="/Scripts/jquery-1.7.2.min.js"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
第1个为Jquery库, 其中第2,3个是验证的。第4个为Ajax提交所用到的。
其中注意一点,当<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script> 如果你不小心引用了2个,
会有什么后果呢? 你点击提交按钮的时候,提交了2次,不信你可以试试。
具体代码如下:
1 @using (Ajax.BeginForm("AddSubmit", "AdminInfo", new AjaxOptions { HttpMethod = "post" })) 2 { 3 <table class="table"> 4 <tr> 5 <td colspan="2" class="TableTitle">创建管理员</td> 6 </tr> 7 <tr> 8 <td>@Html.LabelFor(m => m.UserName)</td> 9 <td class="td2">@Html.TextBoxFor(m => m.UserName)</td> 10 </tr> 11 <tr> 12 <td> </td> 13 <td class="td2">@Html.ValidationMessageFor(m => m.UserName)</td> 14 </tr> 15 16 <tr> 17 <td>@Html.LabelFor(m => m.PassWord)</td> 18 <td class="td2">@Html.PasswordFor(m => m.PassWord)</td> 19 </tr> 20 <tr> 21 <td> </td> 22 <td class="td2">@Html.ValidationMessageFor(m => m.PassWord)</td> 23 </tr> 24 25 <tr> 26 <td>@Html.LabelFor(m => m.TrueName)</td> 27 <td class="td2">@Html.TextBoxFor(m => m.TrueName)</td> 28 </tr> 29 <tr> 30 <td> </td> 31 <td class="td2">@Html.ValidationMessageFor(m => m.TrueName)</td> 32 </tr> 33 34 <tr> 35 <td> </td> 36 <td class="td2"> 37 <input type="submit" value="提交" /></td> 38 </tr> 39 </table> 40 }
如果你需要再表单中带其他参数则方法如下:
@using (Ajax.BeginForm("AddSubmit", "AdminInfo", new {sid = Model.Id }, new AjaxOptions { HttpMethod = "post" }))
2.Html直接提交,代码如下
@using (Html.BeginForm("AddSubmit", "AdminInfo", FormMethod.Post)) { <table class="table"> <tr> <td colspan="2" class="TableTitle">创建管理员</td> </tr> <tr> <td>@Html.LabelFor(m => m.UserName)</td> <td class="td2">@Html.TextBoxFor(m => m.UserName)</td> </tr> <tr> <td> </td> <td class="td2">@Html.ValidationMessageFor(m => m.UserName)</td> </tr> <tr> <td>@Html.LabelFor(m => m.PassWord)</td> <td class="td2">@Html.PasswordFor(m => m.PassWord)</td> </tr> <tr> <td> </td> <td class="td2">@Html.ValidationMessageFor(m => m.PassWord)</td> </tr> <tr> <td>@Html.LabelFor(m => m.TrueName)</td> <td class="td2">@Html.TextBoxFor(m => m.TrueName)</td> </tr> <tr> <td> </td> <td class="td2">@Html.ValidationMessageFor(m => m.TrueName)</td> </tr> <tr> <td> </td> <td class="td2"> <input type="submit" value="提交" /></td> </tr> </table> }
如果你需要再表单中带其他参数则方法如下:
@using (Html.BeginForm("AddSubmit", "AdminInfo", new { sid = 1 }, FormMethod.Post))
3.你可以自己写Jquery提交表单,详细Jquery大家都是会写的,这里就不详细说明了 0-0
本群提供ASP.NET MVC,EF,LINQ,WEB API技术支持,不在乎人多,在乎人精。
ASP.NET MVC群 171560784
诚邀各路高手、初学者加入。