属性不适用于模型ASP.asp.net MVC 5
本文关键字:net MVC asp ASP 不适用 适用于 模型 属性 | 更新日期: 2023-09-27 18:04:50
我在我的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; }
}
}
And Here is my View:
@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>
}
问题是模型的所有属性都不起作用。我已勾选以下选项:我在我的网上有这个值。appsettings下的配置文件:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
2-我已经检查了ModelState。在我的控制器中是有效的。奇怪的是,我有一个类似的代码为我的登录,它没有问题。我应该说我是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" })
视图中的所有属性都需要这样做