How to retrieve form values from HTTPPOST, dictionary or?

Richard picture Richard · Feb 23, 2011 · Viewed 252.1k times · Source

I have an MVC controller that has this Action Method:

[HttpPost]
public ActionResult SubmitAction()
{
     // Get Post Params Here
 ... return something ...
}

The form is a non-trivial form with a simple textbox.

Question

How I access the parameter values?

I am not posting from a View, the post is coming externally. I'm assuming there is a collection of key/value pairs I have access to.

I tried Request.Params.Get("simpleTextBox"); but it returns error "Sorry, an error occurred while processing your request.".

Answer

Darin Dimitrov picture Darin Dimitrov · Feb 23, 2011

You could have your controller action take an object which would reflect the form input names and the default model binder will automatically create this object for you:

[HttpPost]
public ActionResult SubmitAction(SomeModel model)
{
    var value1 = model.SimpleProp1;
    var value2 = model.SimpleProp2;
    var value3 = model.ComplexProp1.SimpleProp1;
    ...

    ... return something ...
}

Another (obviously uglier) way is:

[HttpPost]
public ActionResult SubmitAction()
{
    var value1 = Request["SimpleProp1"];
    var value2 = Request["SimpleProp2"];
    var value3 = Request["ComplexProp1.SimpleProp1"];
    ...

    ... return something ...
}