我无法在我的表单中显示MVC验证消息,控制器将模型状态验证为false,但不显示验证消息。
我的模特:
public class MyViewModel
{
[Display(Name = "Admin User")]
[StringLength(30, MinimumLength = 3)]
[Required(ErrorMessage = "Admin User Account Details are Required")]
public string AdminUser { get; set; }
public string AdminPassword { get; set; }
}
我的观点:
@using (Ajax.BeginForm("AdminPage", "AdminPage", new AjaxOptions { UpdateTargetId = "result" }))
{
<div id="result"></div>
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.AdminUser)<br />
@Html.TextBoxFor(model => model.AdminUser, new { @placeholder = "Username", @class = "short" })
@Html.ValidationMessageFor(model => model.AdminUser)
@Html.TextBoxFor(model => model.AdminPassword, new { @placeholder = "Password", @type = "password", @class = "short" })
<input type="submit" value="Submit">
}
我的控制器:
[HttpPost]
public ActionResult AdminPage(MyViewModel model)
{
if (ModelState.IsValid)
{
return Content("VALID!", "text/html");
}
return Content("NOT VALID!", "text/html");
}
同样在Web.Config中我有:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
我引用了以下脚本:
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
当AdminUser字段为空时,控制器将“NOT VALID”状态返回到“result”div。但是我期待AdminUser显示验证失败消息,但它没有?
要添加的一件事是AdminPage是局部视图。