Reputation: 192
I have This Model in my web application:
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>
}
The Problem is none of the Attributes of The Model are working.I have Checked the following Options: 1-I have this values in my web.config file under the appsettings:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
2-I have checked the ModelState.IsValid in my controller. It is strange that i have a similar code for my login and it has no problem. And I should say That I am new to ASP.NET MVC and I don't Know If There is Anything else i should do. Do You have Any Suggestions for this problem?
Upvotes: 2
Views: 894
Reputation: 33326
Ok there are a few things wrong here.
You are using an overload which will set your error message to be a blank string.
Notice the second parameter below.
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
Simply replace the blank string with a null and it will pull it from the attributes instead:
@Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })
Also you have copied and pasted the same code per property underneath but kept the Password
as the first parameter:
@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" })
You will need to change them to match the controls above and also replace the blank string:
@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" })
This will need to be done for all of the properties within your view.
Upvotes: 2