MVC Ajax Form & Ajax Valida(笔记)
1、引入必要的文件
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
2、Ajax From & Ajax Valida
@using (Ajax.BeginForm("ajax", "home", new AjaxOptions() { UpdateTargetId = "ajaxResult", OnComplete = "return OnComplete()", OnBegin = "return OnBegin();", Confirm = "要提交?", OnFailure = "return OnFailure();", OnSuccess = "OnSuccess" })) { <text>Name:</text><input type="text" id="name" name="name" data-val="true" data-val-required="Name 是必填项." /> <span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="true" style="font-size:large; color:blue;"></span> <br /> <text>Age:</text><input type="text" id="age" name="age" data-val="true" data-val-required="Age 是必填项." data-val-digits="输入合法数字."/> <div class="field-validation-valid" data-valmsg-for="age" data-valmsg-replace="true"></div> <br /> <text>Email:</text><input type="text" id="email" name="email" data-val="true" data-val-email="Email格式不正确."/> <div class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="true"></div> <br /> <input type="submit" value="Ajax提交" /> <br /> } <div id="ajaxResult"> </div> <script> function OnComplete() { alert("OnComplete 事件"); } function OnBegin() { alert("OnBegin 事件"); } function OnFailure() { alert("OnFailure 事件 - ajax请求失败"); } function OnSuccess(data) { alert("ajax 请求成功返回的数据:" + data); } </script>
3、Controllers
public ActionResult AjaxForm() { return View(); } [HttpPost] public ActionResult ajax(string name, string age) { //throw new Exception("内部抛出了异常"); return PartialView("AjaxFromControl", new AjaxModel() { Name = name, Age = age}); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。