2

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?

2
  • When trying to trigger the validation client side there is a validation javascript file in your scripts folder included in the project that has to be included. Commented Jul 27, 2015 at 15:42
  • If you mean jquery.validate.min.js,jquery.validate.unobtrusive.min.js,jquery-1.10.2.min.js and bootstrap.min.js, they are already included in the layout of this view.I also tried to include them in this page regardless of being included in the layout and nothing happend Commented Jul 27, 2015 at 15:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

When i remove the blank string as you said, it gives me this error:Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper<BoardMeeting.Models.ChangePassModel>' does not contain a definition for 'ValidationMessageFor' and the best extension method overload 'System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, string)' has some invalid arguments
@Hooman Sorry the only overload with htmlAttributes seems to need to take the message as an error.
@Hooman Got it, you simply use null instead. I have updated the answer. :)
It worked but the problem was because of the second thing you mentioned and both "" and null works fine. But this only solved the validation problem and the required attribute still don't work.
@Hooman sorry I'm not on a PC now will take a look when I'm back on later. If someone hasn't answered it. 👍🏻

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.