我有一个包含用户创建表单的部分视图。当我使用@Html.Partial("_CreateUser")
在主视图中调用该表单并尝试将其提交为空时,没有任何数据注释验证有效。但是,如果我将该部分视图单独返回给浏览器并再次尝试,则验证消息可以正常工作。任何建议,将不胜感激。代码如下。
视图模型
[Required(ErrorMessage = "Please enter a first name.")]
[Range(2,12, ErrorMessage = "First name must be between {1} and {2}
characters in length.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter a last name.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
部分视图代码段
@using (Html.BeginForm("CreateUser", "OpsManagement"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-3">
<!--- First Name -->
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName,"", new { @class = "text-danger" })
</div>
<div class="col-md-3">
<!--- Last Name -->
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
主视图
<div id="newUserPartialView" class="collapse">
@Html.Partial("_CreateUser")
</div>