Problems to update user information

I'm new to Entity Framework, and I have to update a record in my database. I used the "Edit" example generated by the MVC3 framework and tried to customize to my needs.

I have a password field and before submit it to update I need to encrypt it with MD5. All process is running ok, except for the db.SaveChanges(); it saves the data posted by the form. Doesn't matter if I try to change the password, the framework just ignore that and save the data as it was posted in the form.

My .cshtml file:

<div class="editor-label">
    @Html.Label("password", "Senha")
</div>
<div class="editor-field">
    @Html.Password("password")
</div>

My method:

[HttpPost]
public ActionResult Editar(FormCollection form)
{
    var newPassword = form["password"];
    var email = Session["email"].ToString();

    UserSet user = db.UserSet.SingleOrDefault(m => m.Email == email);

    if (ModelState.IsValid)
    {
        //Changing password
        user.Password = Crypto.CalculateMD5Hash(newPassword);//this line is ignored
        TryUpdateModel(user);
        db.SaveChanges();
        return Redirect("~/Home/Mural");
    }
     return View(user);
}

What am I missing?


Your line

TryUpdateModel(user);

Will overwrite anything you've done on your model prior.

Change the order to

TryUpdateModel(user);
user.Password = Crypto.CalculateMD5Hash(newPassword);//this line is ignored

And it'll probably work.

链接地址: http://www.djcxy.com/p/33572.html

上一篇: 在实体框架中更新排除属性

下一篇: 更新用户信息的问题