1

I'm trying to make a website using asp.net mvc 4 & EF6 where I've Password & Confirm Password fields. If both are same then data will be saved in MSSQL db as usual. I'm using Compare DataAnnotation to compare those two fields & I've added that code in EF6 generated model class. But for some reason, my data is not saving in db. Here are my codes,

Controller

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UserReg(ClientReg regCl)
    {
        if (ModelState.IsValid)
        {
            db.ClientRegs.Add(regCl);
            db.SaveChanges();
            return View();
        else
        {
            return RedirectToAction("Register");
        }
    }

Model

public partial class ClientReg
{
    public int serial { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    [Compare("password")]
    public string ComparePass { get; set; }
}

View

@model MyProj.Models.ClientReg
@using (Html.BeginForm("UserReg", "Home"))
{
    @Html.ValidationSummary(true)
    @Html.AntiForgeryToken()

    <div class="editor-label">
        <strong>Username</strong>
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(a => a.username)
        @Html.ValidationMessageFor(a => a.username)
    </div>
    <div class="editor-label">
        <strong>Password</strong>
    </div>
    <div class="editor-field">
        @Html.PasswordFor(a => a.password)
        @Html.ValidationMessageFor(a => a.password)
    </div>
    <div class="editor-label">
        <strong>Confirm Password</strong>
    </div>
    <div class="editor-field">
        @Html.PasswordFor(a => a.ComparePass)
        @Html.ValidationMessageFor(a => a.ComparePass)
    </div>
    <input type="submit" value="Sign Up" />
}

I can't find any errors in my code. Am I doing something wrong here? How can I compare two password fields by using DataAnnotation? Need this help badly! Thanks.

5
  • Can you show the table you trying to save data on? Commented Sep 23, 2015 at 19:12
  • Why on earth do you have a ComparePass field in your database? You need a Register view model with the ComparePass property and a separate data model without it. Commented Sep 24, 2015 at 0:29
  • @StephenMuecke, thanks. Can you provide a code for your explanation? Commented Sep 24, 2015 at 6:17
  • @Oluwafemi, here is the structure of the table : serial -> int -> auto increment, no null, PK ; username -> varchar(20) -> no null ; password -> varchar(50) -> no null Commented Sep 24, 2015 at 6:18
  • @SinOscuras, Create a new MVC project in VS with forms authentication and look at the code associated with the AccountController and its models. The fact you appear to be storing passwords in the database in plain text is dreadful practice (and if this is a real app, you will need a good lawyer) Commented Sep 24, 2015 at 23:51

1 Answer 1

1

You should separate data access class from view model. The view model should contain the DataAnnotations.Compare attribute.

/* View model */
public partial class ClientRegModel
{
    public int serial { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    [Compare("password")]
    public string ComparePass { get; set; }
}

/* Your DbContext or data class */
public partial class ClientReg
{
    public int serial { get; set; }
    public string username { get; set; }
    public string password { get; set; }
}

You can also checkout this post How to properly implement "Confirm Password" in ASP.NET MVC 3?

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

Comments

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.