views:

301

answers:

4

I don't really know where to look for an error... the situation: I have an ASPX view which contains a form and a few input's, and when I click the submit button everything is POST'ed to one of my ASP.NET MVC actions.

When I set a breakpoint there, it is hit correctly. When I use FireBug to see what is sent to the action, I correctly see data1=abc&data2=something&data3=1234.

However, nothing is arriving in my action method. ViewData is empty, there is no ViewData["data1"] or anything else that would show that data arrived.

How can this be? Where can I start looking for the error?

+6  A: 

ViewData is relevant when going from the controller to the view. It won't post back.

you'll need your action method to look something like

public ActionResult DoSomething(string data1, string data2, int data3) { ...

Then the (model? parameter?) binding should take care of things for you

David Archer
... Exactly :-)
Darin Dimitrov
+1  A: 

Try modifying your Action to accept FormCollection:

public ActionResult DoSomething(FormCollection fc)
{
     System.Diagnostics.Debug.Writeline(fc["data1"]);
}
Robaticus
A: 

If you want to see what is posted to your View, accept FormCollection as a parameter, or bind your form elements directly to a model. Like so:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PostToThisView(FormCollection formItems)
{
    var data1 = formItems["data1"];
    return View();
}

Also, see this question.

JMP
A: 

try this:

Request["data1"]

or

Request.Form["data1"]

or

[HttpPost]
public ActionResult YourPostAction(object data1)

or

[HttpPost]
public ActionResult YourPostAction(ACLassThatHasData1Prop viewmodel)
//your view doesn't has to be strongly typed to the type of the parameter in this case
Omu
Regarding the last example, the model used for the parameter doesn't have to be the same as the model that is strongly-typed to the view from which the request came. The model binder will simply take the values stored in the forms collection (or the query string if the request is a GET) and try to used them to populate whatever type of object is specified as the parameter.
Dr. Wily's Apprentice
@Dr. Wily's Apprentice, thnx for telling me that
Omu