Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 - ViewData empty after POST

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?

like image 284
Alex Avatar asked Jan 28 '26 08:01

Alex


2 Answers

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

like image 129
Dave Archer Avatar answered Jan 30 '26 20:01

Dave Archer


Try modifying your Action to accept FormCollection:

public ActionResult DoSomething(FormCollection fc)
{
     System.Diagnostics.Debug.Writeline(fc["data1"]);
}
like image 26
Robaticus Avatar answered Jan 30 '26 22:01

Robaticus