0

The problem I'm having right now it's that ASP.NET MVC validates the request object, user in this case

public ActionResult Edit(User user)

What I want to do is that if the user's password is left blank, do not update the password, just use the old password, but if it's set, update it.

The problem is that the framework complains that user does not has a password, even if I update the user object, it complains

public ActionResult Edit(User user)
{
    user.Password = "Something";

    // more code...
}

Apparently it does the validation on the request object, is there a way I can skip the validation in this case, or at least delay it until I finished modifying the user object?

This is the full method code

[HttpPost]
public ActionResult Edit(User user)
{    
    if (string.IsNullOrEmpty(user.Password))
    {
        var oldUser = db.Users.Single(u => u.Id == user.Id);
        user.Password = oldUser.Password;
    }

    try
    {
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View(user);
    }
}
9
  • is it a problem of client side validation (something related to ModelState) or database (when you try to submit your changes to db) ? Commented Oct 9, 2012 at 14:25
  • When I submit the form, it calls the validation before it excecutes my controller method code, the method updates the database, it's not client side. Commented Oct 9, 2012 at 14:28
  • Not sure we understand each other well : you can't enter your controller's method, right ? Commented Oct 9, 2012 at 14:29
  • I enter, it's not Javascript, the form is submitted and a POST request is sent to the server, the framework then excecutes the validation, complains, and then excecutes the method's code, according to the debugger. Commented Oct 9, 2012 at 14:31
  • Well. Show real code, please, I suppose you have a Required attribute on property Password in your class User, am I wrong ? Commented Oct 9, 2012 at 14:34

2 Answers 2

3

The model state will still be invalid even after you set the password to something. After you do that try clear the model state using ModelState.Clear(); Or modify the ModelState accordingly i.e clear the error state only for password property ModelState.Remove("Password");

[HttpPost]
public ActionResult Edit(User user)
{    
    if (string.IsNullOrEmpty(user.Password))
    {
        var oldUser = db.Users.Single(u => u.Id == user.Id);
        user.Password = oldUser.Password;
        ModelState.Clear(); // or change the state accordingly (ModelState.Remove("Password");)
    }
.......
}

From your posts it looks like you are ware of the [Required] attribute on the password field. I suggest you force javascript validation and let the user know that they cannot update the password to be blank. Or if you are updating User information in a form that doesn't include password create a new viewmodel or temp model that does not have the password field and the model is specific to that form. Post to this model and bind to this model. Then update the actual user model by using the data from the temp user model. This will be better practice to follow.

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

Comments

0

The problem here isn't related to the server, it's the client side. I assume you have a Required attribute on the Password property so when MVC generates the view for the model it automatically generates client-side validation AKA Unobtrusive validation.

You could write your own custom attribute to handle your particular scenario, however, that would mean you would also need to write your own client-side validation. The simple fix here is to remove the Required attribute and handle the validation on the server side.

What I want to do is that if the user's password is left blank, do not update the password, just use the old password, but if it's set, update it.

If that is the case then your logic is actually wrong anyway. You shouldn't have the Required attribute on the Password as your rules dictate that it can be empty.

2 Comments

Unfortunately that's not the problem, I removed the javascript validation and still get the error.
@gosukiwi So you were actually getting to the server? i.e. if you put a break point in your Action you were hitting 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.