Gha*_*han 2 asp.net ajax asp.net-mvc jquery asp.net-mvc-5
我想用jquery发布我的MVC表单.我的观点模型是这样的
public class DemoViewModel
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的控制器是
[HttpPost]
public JsonResult LongRunningDemoProcess(DemoViewModel model)
{
Thread.Sleep(1000);
return Json(model, "json");
}
Run Code Online (Sandbox Code Playgroud)
我的视图有以下代码
@model WebApplication2.Models.DemoViewModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("LongRunningDemoProcess", "Home", FormMethod.Post,
new { encType = "multipart/form-data", id = "myform", name = "myform" }))
{
<div class="form-group">
@Html.LabelFor(model => model.FirstName,
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName,
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<input type="submit" name="operation" id="process" value="process" />
}
<div id="divProcessing">
<p>Processing, please wait . . . <img src="../../Content/ajax-loader.gif"></p>
</div>
<div id="divResult">
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function () {
// Hide the "busy" Gif at load:
$("#divProcessing").hide();
// Handle the form submit event, and make the Ajax request:
$("#myform").on("submit", function (event) {
event.preventDefault();
// Show the "busy" Gif:
$("#divProcessing").show();
var url = $(this).attr("action");
var formData = $(this).serialize();
$.ajax({
url: url,
type: "POST",
data: formData,
dataType: "json",
success: function (resp) {
// Hide the "busy" gif:
$("#divProcessing").hide();
// Do something useful with the data:
$("<h3>" + resp.FirstName + " " + resp.LastName + "</h3>").appendTo("#divResult");
}
})
});
});
</script>
}
Run Code Online (Sandbox Code Playgroud)
但问题是,即使它们是必填字段的错误,也会发布表单并获得结果

我试图测试这段代码,if($("#myform").valid())但没有可用的方法.如何使用jquery.validate.js测试验证,因为它已经包含在内.谢谢
由于通过ajax提交表单,您必须手动调用它.
这是您可以采取的一种方法:
$.validator.unobtrusive.parse($form);
$form.validate();
if ($form.valid()) {
// ajax call
}
else {
// Failed show errors
}
Run Code Online (Sandbox Code Playgroud)
如果失败,则错误包含在内,$form.validate().errorList但您必须手动解析它们.
你可以这样做:
$.each($form.validate().errorList, function (key, value) {
$errorSpan = $("span[data-valmsg-for='" + value.element.id + "']");
$errorSpan.html("<span style='color:red'>" + value.message + "</span>");
$errorSpan.show();
});
Run Code Online (Sandbox Code Playgroud)
这只是手动替换您对消息的验证.
我已经通过在捆绑包中包含jquery.validate.unobtrusive.js解决了它
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.validate.unobtrusive.js"
));
Run Code Online (Sandbox Code Playgroud)
然后像这样检查
if ($("#myform").valid()) {
alert('not valid');
}
else{
// ajax logic
}
Run Code Online (Sandbox Code Playgroud)