ValidationMessageFor工作不正常

ValidationMessageFor工作不正常

本文介绍了MVC ValidationMessageFor工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的看法如下:

 <&字段集GT;
        <传奇>用户注册和LT; /传说>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(M = GT; m.UsrName)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.TextBoxFor(M = GT; m.UsrName)
            @ Html.ValidationMessageFor(M = GT; m.UsrName)
        < / DIV>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(M = GT; m.Pwd)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.PasswordFor(M = GT; m.Pwd)
            @ Html.ValidationMessageFor(M = GT; m.Pwd)
        < / DIV>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(M = GT; m.ReEnterPwd)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.PasswordFor(M = GT; m.ReEnterPwd)
            @ Html.ValidationMessageFor(M = GT; m.ReEnterPwd)
        < / DIV>
       <&字段集GT;
         <传奇>位置和LT; /传说>
           &所述;跨度的id =locationDiv>
           @ Html.RadioButtonFor(M = GT; m.Location,LOC1)@ Html.Label(LOC1)
           &安培; NBSP;&安培; NBSP;
           < / SPAN>
           @ Html.RadioButtonFor(M = GT; m.Location中Loc2)@ Html.Label(中Loc2)
           @ Html.ValidationMessageFor(M = GT; m.Location)
        < /字段集>         <&字段集GT;
         <传奇>的角色和LT; /传说>
            @ Html.RadioButtonFor(M = GT; m.Role,用户)@ Html.Label(用户)
           &安培; NBSP;&安培; NBSP; @ Html.RadioButtonFor(M = GT; m.Role,管理)@ Html.Label(管理)
            @ Html.ValidationMessageFor(M = GT; m.Role)
         < /字段集>        &所述p为H.;
            <输入类型=提交值=注册用户/>
        &所述; / P>
    < /字段集>

即使我没有做所有字段填充,但仍进行到即使它们所需的所有控制器。我以为

  @ Html.ValidationMessageFor

应该prevent这一点。

  [必需]
    公共字符串位置{搞定;组; }    [需要]
    公共字符串角色{搞定;组; }    [需要]
    [显示(名称=用户名)]
    公共字符串UsrName {搞定;组; }    [需要]
    [StringLength(50 MinimumLength = 5的ErrorMessage =必须有5最小长度)]
    公共字符串密码{搞定;组; }    [需要]
    [显示(名称=重新输入密码)]
    [StringLength(50 MinimumLength = 5的ErrorMessage =必须有5最小长度)]
    [比较(登记密码,的ErrorMessage =密码,并重新输入的密码不匹配。)
    公共字符串ReEnterPwd {搞定;组; }


解决方案

您必须在视图中包括以下脚本:

 <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< /脚本>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>

I have the following in my view:

     <fieldset>
        <legend>User Registration</legend>
        <div class="editor-label">
            @Html.LabelFor(m => m.UsrName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.UsrName)
            @Html.ValidationMessageFor(m => m.UsrName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.Pwd)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Pwd)
            @Html.ValidationMessageFor(m => m.Pwd)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.ReEnterPwd)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.ReEnterPwd)
            @Html.ValidationMessageFor(m => m.ReEnterPwd)
        </div>
       <fieldset>
         <legend>Location</legend>
           <span id="locationDiv">
           @Html.RadioButtonFor(m => m.Location, "Loc1")  @Html.Label("Loc1")
           &nbsp;&nbsp;
           </span>
           @Html.RadioButtonFor(m => m.Location, "Loc2") @Html.Label("Loc2")
           @Html.ValidationMessageFor(m => m.Location)
        </fieldset>

         <fieldset>
         <legend>Role</legend>
            @Html.RadioButtonFor(m => m.Role, "User") @Html.Label("User")
           &nbsp;&nbsp; @Html.RadioButtonFor(m => m.Role, "Admin") @Html.Label("Admin")
            @Html.ValidationMessageFor(m => m.Role)
         </fieldset>

        <p>
            <input type="submit" value="Register User" />
        </p>
    </fieldset>

Even if I don't have all the fields filled, it still goes to the controller even though they are all required. I thought

    @Html.ValidationMessageFor

was supposed to prevent that.

    [Required]
    public string Location { get; set; }

    [Required]
    public string Role { get; set; }

    [Required]
    [Display(Name = "User Name")]
    public string UsrName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Must have a minimum length of 5.")]
    public string Pwd { get; set; }

    [Required]
    [Display(Name = "Re-enter Password")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Must have a minimum length of 5.")]
    [Compare("Pwd", ErrorMessage = "The password and re-entered password do not match.")]
    public string ReEnterPwd { get; set; }
解决方案

You must include the following scripts in the view:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

这篇关于MVC ValidationMessageFor工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:58