我掙扎了好幾個小時,試圖理解爲什麼我在表單中呈現的輸入元素不再包含data-val屬性。 環境是MVC 5和.NET Framework 4.5(I從MVC 4升級希望解決問題!)不顯示不顯示驗證的ASP.NET MVC數據val屬性
所呈現的字段中的一個的一個例子:
<input id="UserName" class="text-box single-line" type="text" value="" name="UserName">
</input>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="UserName"></span>
我的模型:
public class AccountRegistrationValidation
{
[Key]
public int ID { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[System.Web.Mvc.Remote("UsernameInUse", "Account", HttpMethod = "POST",
ErrorMessageResourceType = typeof(ValidationStrings), ErrorMessageResourceName = "UsernameExists")]
public string UserName { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[StringLength(20, ErrorMessageResourceName = "PasswordMinLength", ErrorMessageResourceType = typeof(ValidationStrings), MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[DataType(DataType.Password)]
[System.Web.Mvc.Compare("Password", ErrorMessageResourceName = "PasswordsMustMatch", ErrorMessageResourceType = typeof(ValidationStrings))]
public string ConfirmPassword { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[DataType(DataType.EmailAddress)]
[System.Web.Mvc.Remote("EmailInUse", "Account", HttpMethod = "POST", ErrorMessage = "Email address already exists. Please enter a different address.")]
public string Email { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ValidationStrings))]
[DataType(DataType.Text)]
public string LastName { get; set; }
}
觀形態:
@model Test.Models.AccountRegistrationValidation
@using CaptchaMvc.HtmlHelpers
@{
ViewBag.Title = ViewRes.RegisterStrings.RegisterTitle;
}
<div class="container">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>@ViewBag.Title <br /><small>@ViewRes.RegisterStrings.RegisterSubTitle</small></h1>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span7">
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>@ViewRes.RegisterStrings.FillInFields</legend>
<div>
<p class="text-info"><i>@ViewRes.RegisterStrings.ALL_FIELDS_REQ</i></p>
</div>
<div class="control-group">
@Html.LabelFor(m => m.FirstName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.FirstName)
</div>
@Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="control-group">
@Html.LabelFor(m => m.LastName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.LastName)
</div>
@Html.ValidationMessageFor(m => m.LastName)
</div>
<div class="control-group">
@Html.LabelFor(m => m.UserName, new { @class = "control-label" })
<div class="controls">
@Html.EditorFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.Password, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.Password)
</div>
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="control-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.ConfirmPassword)
</div>
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<div class="control-group">
@Html.LabelFor(m => m.Email, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.Email)
</div>
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="control-group">
<label class="control-label" for="Input">@ViewRes.RegisterStrings.SEC_CONTROL</label>
<div class="controls">
@Html.Captcha(ViewRes.RegisterStrings.REFRESH_CAPTCHA, "Input", 5, "Is required field.")
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="@ViewRes.RegisterStrings.ButRegister" class="btn btn-success btn-large" />
</div>
</div>
</fieldset>
}
</div>
<div class="span1">
</div>
</div>
<div class="row">
<div class="span12">
<div class="muted"><small>@Html.Raw(ViewRes.RegisterStrings.IntroTermsService)</small></div>
</div>
</div>
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
我查看包括所有必需的js文件(jquery.validate和jquery.validate.unobtrusive)
任何想法?!?
您是否在web.config中將'ClientValidationEnabled'設置爲'true'? – nemesv
是的,它已啓用 – marfin
任何運氣與此。這正是我們正在經歷的......是我們MVC5升級的結果。 – BlackjacketMack