Jquery post 和不显眼的 ajax 验证不工作 mvc 4
Jquery post and unobtrusive ajax validation not working mvc 4
在 jquery 回发中,如果模型状态无效,我想使用 jquery 非干扰性验证来显示验证错误消息。我创建了一个示例应用程序。应用中的viewmodel如下
public class CityModel
{
public int Id { get; set; }
[Display(Name = "City")]
[Required(ErrorMessage = "City is required")]
public string City { get; set; }
}
控制器有以下动作方法
public ActionResult City()
{
var cities = GetCities();
return View(cities);
}
[HttpPost]
public ActionResult SaveCity(CityModel cityModel)
{
if (ModelState.IsValid)
{
//Save City
return null;
}
else
{
return View();
}
}
public List<CityModel> GetCities()
{
var citiesList = new List<CityModel>
{
new CityModel() {Id = 1, City = "London"},
new CityModel() {Id = 2, City = "New York"}
};
return citiesList;
}
观看次数有以下标记
@model List<CityModel>
<h2>Cities</h2>
@Html.ValidationSummary(true)
@using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "frmSite", name = "frmSite" }))
{
@Html.EditorForModel()
<input type="submit" name="Sumbit" onclick=" SaveCity(); return false; " />
}
<script>
function SaveCity() {
$.ajax({
type: "POST",
url: "/Home/SaveCity",
contentType: "application/html; charset=utf-8",
data: {
Id: $('.cityId').val(),
City: $('.cityName').val()
},
success: function (data) {
}
});
}
</script>
编辑器模板看起来像
@model CityModel
<table class="table">
<tr>
<td>
@Html.TextBoxFor(x => x.Id, new {@class = "cityId"})
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(x => x.City, null, new {@class = "cityName"})
@Html.ValidationMessageFor(x => x.City)
</td>
</tr>
</table>
我检查过 <script src="/Scripts/jquery-1.10.2.js"></script> <script src="/Scripts/jquery.validate.js"></script> <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
包含在页面中并且 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
存在于网络配置文件中
您正在通过 ajax 调用您的 post,因此您需要手动调用 $form.validate();
并使用 $form.valid()
:
测试结果
function SaveCity() {
$.validator.unobtrusive.parse($form);
$form.validate();
if ($form.valid()) {
$.ajax({
type: "POST",
url: "/Home/SaveCity",
contentType:"application/json; charset=utf-8",
data: {
Id: $('.cityId').val(),
City: $('.cityName').val()
},
success: function (data) {
}
});
}
}
如果是纯粹的客户端,错误将包含在 jquery 验证对象中 $form.validate().errorList
但您将不得不进行一些类似于我在下面提到的手动处理。
如果您希望 return 服务器端模型状态,您可以将模型状态错误作为键值对添加到您的控制器中,并 return 它们作为 json。
您可以使用以下方法显示消息。
这会找到所有带有模型状态错误键的验证消息范围,并向它们添加红色错误消息。请注意,您可能希望对此进行调整以针对某个键显示许多错误消息。
public doValidation(e)
{
if (e.data != null) {
$.each(e.data, function (key, value) {
$errorSpan = $("span[data-valmsg-for='" + key + "']");
$errorSpan.html("<span style='color:red'>" + value + "</span>");
$errorSpan.show();
});
}
}
已更新
这是上面的改编,因此您可以从 jquery 不显眼的错误中手动解析它:
$.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();
});
只需将其弹出到 else 语句中即可。 :)
在 jquery 回发中,如果模型状态无效,我想使用 jquery 非干扰性验证来显示验证错误消息。我创建了一个示例应用程序。应用中的viewmodel如下
public class CityModel
{
public int Id { get; set; }
[Display(Name = "City")]
[Required(ErrorMessage = "City is required")]
public string City { get; set; }
}
控制器有以下动作方法
public ActionResult City()
{
var cities = GetCities();
return View(cities);
}
[HttpPost]
public ActionResult SaveCity(CityModel cityModel)
{
if (ModelState.IsValid)
{
//Save City
return null;
}
else
{
return View();
}
}
public List<CityModel> GetCities()
{
var citiesList = new List<CityModel>
{
new CityModel() {Id = 1, City = "London"},
new CityModel() {Id = 2, City = "New York"}
};
return citiesList;
}
观看次数有以下标记
@model List<CityModel>
<h2>Cities</h2>
@Html.ValidationSummary(true)
@using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "frmSite", name = "frmSite" }))
{
@Html.EditorForModel()
<input type="submit" name="Sumbit" onclick=" SaveCity(); return false; " />
}
<script>
function SaveCity() {
$.ajax({
type: "POST",
url: "/Home/SaveCity",
contentType: "application/html; charset=utf-8",
data: {
Id: $('.cityId').val(),
City: $('.cityName').val()
},
success: function (data) {
}
});
}
</script>
编辑器模板看起来像
@model CityModel
<table class="table">
<tr>
<td>
@Html.TextBoxFor(x => x.Id, new {@class = "cityId"})
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(x => x.City, null, new {@class = "cityName"})
@Html.ValidationMessageFor(x => x.City)
</td>
</tr>
</table>
我检查过 <script src="/Scripts/jquery-1.10.2.js"></script> <script src="/Scripts/jquery.validate.js"></script> <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
包含在页面中并且 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
存在于网络配置文件中
您正在通过 ajax 调用您的 post,因此您需要手动调用 $form.validate();
并使用 $form.valid()
:
function SaveCity() {
$.validator.unobtrusive.parse($form);
$form.validate();
if ($form.valid()) {
$.ajax({
type: "POST",
url: "/Home/SaveCity",
contentType:"application/json; charset=utf-8",
data: {
Id: $('.cityId').val(),
City: $('.cityName').val()
},
success: function (data) {
}
});
}
}
如果是纯粹的客户端,错误将包含在 jquery 验证对象中 $form.validate().errorList
但您将不得不进行一些类似于我在下面提到的手动处理。
如果您希望 return 服务器端模型状态,您可以将模型状态错误作为键值对添加到您的控制器中,并 return 它们作为 json。
您可以使用以下方法显示消息。
这会找到所有带有模型状态错误键的验证消息范围,并向它们添加红色错误消息。请注意,您可能希望对此进行调整以针对某个键显示许多错误消息。
public doValidation(e)
{
if (e.data != null) {
$.each(e.data, function (key, value) {
$errorSpan = $("span[data-valmsg-for='" + key + "']");
$errorSpan.html("<span style='color:red'>" + value + "</span>");
$errorSpan.show();
});
}
}
已更新
这是上面的改编,因此您可以从 jquery 不显眼的错误中手动解析它:
$.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();
});
只需将其弹出到 else 语句中即可。 :)