我的Web应用程序中有此模型:

    namespace BoardMeeting.Models
{
    public class ChangePassModel
    {
        [Required(ErrorMessage = "password must be entered")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public string Username { get; set; }

        [Required(ErrorMessage = "you must enter the new password")]
        [DataType(DataType.Password)]
        public string NewPass { set; get; }

        [DataType(DataType.Password)]
        [Compare("NewPass", ErrorMessage = "The Passwords Do not match")]
        public string ConfimedPass { set; get; }
    }
}


这是我的观点:

@model BoardMeeting.Models.ChangePassModel

@{

    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{

    <div class="form-group ">
        <div class="row" dir="rtl">
            <div>
                @Html.Label("username :", new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.LabelFor(m => m.Username, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.Label("user :", new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.LabelFor(m => m.Username, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2  pull-right", style = "margin-top:5px;" })
            </div>
        </div>

    </div>

    <div class="form-group">
        <div class="row" dir="rtl">
            <div>
                @Html.Label("old password", new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.TextBoxFor(m=>m.Password, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right form-control", style = "margin-top:5px;" })
                @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="row" dir="rtl">
            <div>
                @Html.Label("new password", new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.TextBoxFor(m => m.NewPass, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right form-control", style = "margin-top:5px;" })
                @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="row" dir="rtl" style="align-content:center">
            <div>
                @Html.Label("confirm new password", new { @class = "col-xs-12 col-md-4 col-sm-3 col-lg-2 pull-right", style = "margin-top:5px;" })
            </div>
            <div>
                @Html.TextBoxFor(m => m.ConfimedPass, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right form-control", style = "margin-top:5px;",type="password" })
                @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="submit" class="btn btn-success pull-right" />
        </div>
    </div>
}


问题不在于模型的属性没有起作用,我已经检查了以下选项:
1-我在appsettings下的web.config文件中具有以下值:

<add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />


2-我已经在控制器中检查了ModelState.IsValid。
奇怪的是我有一个类似的登录代码,没有问题。
我应该说,我是ASP.NET MVC的新手,我不知道还有什么我应该做的。您对此问题有任何建议吗?

最佳答案

好的,这里有些错误。

您正在使用重载,这会将您的错误消息设置为空白字符串。

注意下面的第二个参数。

@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })


只需将空字符串替换为null,它将从属性中拉出它:

@Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })


同样,您已经在每个属性下面复制并粘贴了相同的代码,但将Password保留为第一个参数:

@Html.TextBoxFor(m => m.NewPass, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right form-control", style = "margin-top:5px;" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })


您将需要更改它们以匹配上面的控件,并替换空白字符串:

@Html.TextBoxFor(m => m.NewPass, new { @class = "col-xs-12 col-md-2 col-sm-3 col-lg-2 pull-right form-control", style = "margin-top:5px;" })
@Html.ValidationMessageFor(m => m.NewPass, null, new { @class = "text-danger" })


这将需要对视图中的所有属性进行。

关于c# - 属性不适用于模型ASP.NET MVC 5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31657177/

10-16 11:55
查看更多