views:

1020

answers:

2

When a form is posted back to the server, is it possible to manipulate, change, set the values contained in HTTP Post in the controller action? I would like to remove certain textbox values entered by the user so that these values always have to be re-entered (e.g. password fields). By default Html helpers extract initial values for HTML controls from the HTTP Post info.

A: 

You can create a custom ModelBinder that will allow you to manipulate posted data.

ScottGu's post that covers this subject.

Olivier PAYEN
+6  A: 

You don't need a custom ModelBinder.

    [Bind(Exclude="Foo,Bar")]
    public ActionResult Insert(T model)

Now Foo and Bar are null.

This does what you ask, but I'm not actually sure it's what you meant. :)

My guess is that your action does need to see the password (or whatever) entered by the user. But if, for example, a different field needs to be re-entered, you don't want to populate the password when you re-display the form. That's a good idea. But in this case, model binders don't even enter in. You simply set the field to null before you re-display the view.

    public ActionResult Insert(T model)
    {
        try
        {
            Repository.Add(model);
        }
        catch (Exception ex)
        {
            ViewData["Message"] = ex.Message;
            model.Password = null;
            return View(model);
        }
        // success!
        return RedirectToRoute( //...
   }
Craig Stuntz