Asp.net mvc MVC4:ModelState.IsValid始终返回true
我有一个PartialView,它包含一个表单,可以异步地将数据发布到我的控制器。如果Asp.net mvc MVC4:ModelState.IsValid始终返回true,asp.net-mvc,validation,asp.net-mvc-4,model-validation,Asp.net Mvc,Validation,Asp.net Mvc 4,Model Validation,我有一个PartialView,它包含一个表单,可以异步地将数据发布到我的控制器。如果ModelState有效,控制器将添加用户,如果无效,它将返回带有无效模型的PartialView。我遇到的问题是无论发生什么情况,ModelState始终有效。我可以看到表单正在正确序列化,并且正在填充DynamicActionUserModel.RegisterModel的所有属性 我不明白为什么会出现这种情况,但有没有可能模型绑定不起作用,因为我在模型中有一个模型 这是我的密码 查看 @model IEn
ModelState
有效,控制器将添加用户,如果无效,它将返回带有无效模型的PartialView。我遇到的问题是无论发生什么情况,ModelState
始终有效。我可以看到表单正在正确序列化,并且正在填充DynamicActionUserModel.RegisterModel
的所有属性
我不明白为什么会出现这种情况,但有没有可能模型绑定不起作用,因为我在模型中有一个模型
这是我的密码
查看
@model IEnumerable<RobotDog.Models.UserModel>
<!-- other stuff -->
<div class="createUser">
@Html.Partial("_UserPartial", new DynamicActionUserModel { Action = "CreateUser", RegisterModel = new RegisterModel() })
</div>
<script type="text/javascript">
$(function () {
$('form.ajax').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
location.reload(true);
} else {
$('.createUser').html(result);
}
}
});
}
return false;
});
});
</script>
@model RobotDog.Models.DynamicActionUserModel
@using(Html.BeginForm(Model.Action,"Admin", FormMethod.Post, new { @class = "ajax" })) {
@Html.ValidationSummary(true, "Registration unsuccessful. Please correct errors and try again.")
@Html.TextBoxFor(x => x.RegisterModel.Email, new { @class = "input-xlarge", @placeholder = "Email"})
@Html.TextBoxFor(x => x.RegisterModel.UserName, new { @class = "input-xlarge", @placeholder = "User Name"})
@Html.PasswordFor(x => x.RegisterModel.Password, new { @class = "input-xlarge", @placeholder = "Password"})
@Html.ListBoxFor(x => x.RegisterModel.SelectedRoles, Model.RegisterModel.Roles)
<input type="submit" value="Submit" class="btn"/>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器
[HttpPost]
public ActionResult CreateUser(DynamicActionUserModel model) {
if (!ModelState.IsValid) {
foreach(var value in ModelState.Values) {
foreach (var error in value.Errors) {
ModelState.AddModelError(string.Empty, error.ErrorMessage);
}
}
return PartialView("_UserPartial", model);
}
try {
...
return Json(new { success = true });
}
catch(Exception e) { ... }
return PartialView("_UserPartial", model);
}
如果将AllowEmptyStrings=false去掉,会发生什么?仅供参考,这是默认值,MVC忽略此属性(其目的是用于DynamicData)。不过,我看到它会导致奇怪的验证问题。顺便说一下,你不应该使用@placeholder,因为类在c#中是保留字,所以在类中使用@placeholder,在非保留属性名中不使用它。你的布局中不包括jquery.validation吗?我不确定是否按您的方式包含它可能会导致问题,因为它可能会被包含在视图模型中。在视图模型中进行数据访问也是一种不好的做法。我假设Roles属性从数据库中加载了一些内容?但不应该引起这个问题。这种做法之所以不好,是因为它能有效地让视图数据库感知。@MystereMan谢谢,这里有很多好信息。我正在学习mvc框架和这条非常受欢迎的建议。如果你接受AllowEmptyStrings=false,会发生什么?仅供参考,这是默认值,MVC忽略此属性(其目的是用于DynamicData)。不过,我看到它会导致奇怪的验证问题。顺便说一下,你不应该使用@placeholder,因为类在c#中是保留字,所以在类中使用@placeholder,在非保留属性名中不使用它。你的布局中不包括jquery.validation吗?我不确定是否按您的方式包含它可能会导致问题,因为它可能会被包含在视图模型中。在视图模型中进行数据访问也是一种不好的做法。我假设Roles属性从数据库中加载了一些内容?但不应该引起这个问题。这种做法之所以不好,是因为它能有效地让视图数据库感知。@MystereMan谢谢,这里有很多好信息。我正在学习mvc框架和这个非常受欢迎的建议。
[HttpPost]
public ActionResult CreateUser(DynamicActionUserModel model) {
if (!ModelState.IsValid) {
foreach(var value in ModelState.Values) {
foreach (var error in value.Errors) {
ModelState.AddModelError(string.Empty, error.ErrorMessage);
}
}
return PartialView("_UserPartial", model);
}
try {
...
return Json(new { success = true });
}
catch(Exception e) { ... }
return PartialView("_UserPartial", model);
}