简体   繁体   中英

ASP.NET MVC - Manipulating HTTP Post

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 (eg password fields). By default Html helpers extract initial values for HTML controls from the HTTP Post info.

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( //...
   }

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

ScottGu's post that covers this subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM