1

Can you help me to build my "ChangePassword" View in MVC3 ?

Here what I have tried to do:

ProfileTeacherController.cs

public ViewResult ChangePassword(int id)
    {
        var user = User.Identity.Name;
        int inter = int.Parse(user);

        var teachers = from t in db.Teachers
                       where t.AffiliationNumber == inter
                       select t;

        Teacher teacher = new Teacher();
        foreach (var teach in teachers)
        {
            teacher = teach;
        }

        return View(teacher);
    }

    [HttpPost]
    public ActionResult ChangePassword(Teacher teacher)
    {
        if (ModelState.IsValid)
        {
            // How can I compare the two fields password in my view ?
            db.Entry(teacher).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Edit", "ProfileTeacher", new { id = teacher.TennisClubID });
        }
        return View(teacher);
    }

Here the ChangePassword (View)

@model TennisOnline.Models.Teacher
@{
ViewBag.Title = "ChangePassword";
}

<h2>Changement du mot de passe</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend></legend>

    <div class="editor-label">
        @Html.Label("Enter the new password")
    </div>
    <div class="editor-field">
        @Html.PasswordFor(model => model.Pin, new { value = Model.Pin })
    </div>

    <div class="editor-label">
        @Html.Label("Confirm your password")
    </div>
    <div class="editor-field">
        @Html.Password("ConfirmPassword")
    </div>


    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

So, How can I verify in my controller if the two passwords are the same, please ? Thanks in advance

2 Answers 2

5

In addition you can add the message to be shouwn when the two password are not the same in the Compare attribute.

[Compare("NewPassword", ErrorMessage = "The new password and confirm password do not match.")] 
3

I would recommend the usage of a view model:

public class TeacherViewModel
{
    ...

    [Compare("ConfirmPassword")]
    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
}

now have your view take the view model and also your Post action.

In addition to that in your GET action you seem to have written some foreach loop which I don't see its usage. You could simplify:

[Authorize]
public ViewResult ChangePassword(int id)
{
    var user = User.Identity.Name;
    int inter = int.Parse(user);
    var teacher = db.Teachers.SingleOrDefault(t => t.AffiliationNumber == inter);
    return View(teacher);
}
1
  • Thank you It works great ! And thanks also for the tips about the "foreach loop"
    – Razor
    Apr 23, 2012 at 9:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.