2013-12-21 146 views
3

我掙扎了好幾個小時,試圖理解爲什麼我在表單中呈現的輸入元素不再包含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)

任何想法?!?

+1

您是否在web.config中將'ClientValidationEnabled'設置爲'true'? – nemesv

+0

是的,它已啓用 – marfin

+1

任何運氣與此。這正是我們正在經歷的......是我們MVC5升級的結果。 – BlackjacketMack

回答

-1

您需要做的是在您的控制器中插入以下代碼。它所做的是驗證模型中的所有要求是否都滿足。

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

這不能回答這個問題! OP的問題在於驗證屬性不是在HTML視圖中呈現的,它沒有任何操作系統在控制器中的內容。 – nemesv

+0

抱歉,我想知道你的問題。你有沒有想過忽略C#剃鬚刀,而是插入你想要的HTML頁面的直接HTML輸入代碼? – user2138160

+0

問題在於助手完成的渲染。這真的很奇怪。我沒有用於字符串的EditorTemplates,但是在渲染輸入字段時,這個項目看起來有點不同 – marfin

0

我有完全一樣的問題 - ValidationMessageFor()跨度均呈現正常,但數據-VAL *上的輸入字段屬性是無處可看。在我們上週更新了一系列NuGet軟件包之前,所有的工作都很完美。

我們使用帶有FluentValidation的NInject來爲我們的應用程序的特定區域提供一些自定義驗證器。隱藏在這些自定義驗證綁定是以下行: -

kernel.Unbind<ModelValidatorProvider>(); 

我們評論這條線的,並且數據-VAL *屬性是回來了,一切都太棒了一次。我們最近升級了所有的Nu​​Get包(主要是從MVC 5.0.0.0升級到MVC 5.2.0.0和WebApi 2.2)後才發現這很明顯。一路走來,許多其他軟件包升級,包括FluentValidation和NInject。

這行代碼沒有改變,所以有可能NInject的行爲在版本3.0和3.2之間改變了,因爲它在更新之前一切正常。

我的建議 - 檢查你的IoC綁定,因爲它可能是你正在攔截默認模型驗證行爲的地方。