tags:

views:

88

answers:

2

Currently the way I am handling postbacks in ASP.NET MVC is to grab the input variables using:

string username = "";

if (null != Request["username"])
     username = Request["username"].ToString();

I then would run a regex on the variable to ensure it was valid.

Is there any other method for doing this?

+1  A: 

ASP.NET MVC does Request-To-Object mapping automatically, through ModelBinders. An older article is here, under "Form Post and Model Binder Improvements", and there is a video here.

Michael Stum
Yes that is what I recall reading, but what if the page has more inputs in it that don't directly map to a particular object? Mix and match then?
mrblah
+1  A: 

You can handle the form inputs in your Action this way:

public ActionResult Create(string username)
{
  // use
}

but you need to set your Route:

routes.MapRoute(
                "Default",                                              // Route name
                "Create/{username}",                           // URL with parameters
                new { controller = "YourController", action = "Create", username = "" }  // Parameter defaults
            );

Or you can use ModelBinders

Marwan Aouida