用于不引人注目的验证的ASP.NET MVC data-val属性不存在

时间:2013-12-21 13:10:59

标签: jquery asp.net-mvc unobtrusive-validation

我正在努力理解为什么我在表单中呈现的输入元素不再包含data-val属性。 环境是MVC 5和.net framework 4.5(我从MVC 4升级,希望能解决问题!)

正在呈现的一个字段的示例:

<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)

有什么想法吗?!?

2 个答案:

答案 0 :(得分:0)

我有完全相同的问题 - ValidationMessageFor()跨度渲染正常,但输入字段上的data-val *属性无处可见。在我们上周更新了大量NuGet软件包之前,一切都运行良好。

我们正在使用NInject和FluentValidation,为我们的应用程序的特定区域提供一些自定义验证器。隐藏在这些自定义验证器的绑定中的是以下行: -

kernel.Unbind<ModelValidatorProvider>();

我们评论了这一行,并且data-val *属性又回来了,一切都很棒了。

事后看来这很明显,但最近我们升级了所有NuGet软件包(主要是从MVC 5.0.0.0升级到MVC 5.2.0.0和WebApi 2.2)时才会出现这种情况。在此过程中,许多其他软件包已升级,包括FluentValidation和NInject。

这行代码没有改变,因此NInject的行为可能在版本3.0和3.2之间发生了变化,因为在更新之前一切正常。

我的建议 - 检查您的IoC绑定,因为您可能正在某处拦截默认的模型验证行为。

答案 1 :(得分:-1)

您需要做的是在控制器中插入以下代码。它的作用是验证模型中的所有要求是否都满足。

    public ActionResult Register(Models.MyModel Model)
    { 
      if (ModelState.IsValid)
      {
      }
    }