3

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?

Alex
  • 75,813
  • 86
  • 255
  • 348
  • wrt data1=abc&data2=... what does the method signature for your action method look like? – Dave Archer May 28 '10 at 19:09
  • No parameters. ViewData["data1"] should be able to access the value. But it's null, even though FireBug shows that the value was submitted. – Alex May 28 '10 at 19:10
  • What do you expect to find in ViewData in a postback? It is used for the controller to send data to the view and not vice-versa. Look in `Request["data1"]` or even better in the parameters of the controller action. – Darin Dimitrov May 28 '10 at 19:10
  • Clearly I didn't have enough sleep last night. Thanks. – Alex May 28 '10 at 19:13
  • @Alex, @Darin is 100% correct, esp with the adive that you should use parameters. shy away from magic strings etc – Dave Archer May 28 '10 at 19:15
  • 3
    you know guys, actually there is no postback in mvc :), it's just post – Omu May 28 '10 at 19:16
  • 1
    @David: I was actually using parameters in my original function but couldn't get model binding to work, then created a test method without parameters, and the rest is history. Of course the FormCollection shouldn't be accessed directly. – Alex May 28 '10 at 19:17
  • 2
    @Omu, yeah no postback in mvc and thanks God :-) Good remark. – Darin Dimitrov May 28 '10 at 19:19
  • Guys, if ViewData is not posted from a view to a controller, then what is really meant in the [Remarks](https://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.viewdata%28v=vs.118%29.aspx) section of the docs? This: "The view can add to or change the data, which will be sent to the controller when the view is posted as part of a request." I have not used it and I have no idea how it could work. I just would like to understand what is meant in the docs. – Daniil Veriga Jan 20 '16 at 12:49

4 Answers4

8

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

Dave Archer
  • 3,022
  • 20
  • 23
1

Try modifying your Action to accept FormCollection:

public ActionResult DoSomething(FormCollection fc)
{
     System.Diagnostics.Debug.Writeline(fc["data1"]);
}
Robaticus
  • 22,857
  • 5
  • 54
  • 63
0

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.

Community
  • 1
  • 1
JMP
  • 7,734
  • 6
  • 33
  • 34
0

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
  • 69,856
  • 92
  • 277
  • 407
  • 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 May 28 '10 at 20:36
  • @Dr. Wily's Apprentice, thnx for telling me that – Omu May 29 '10 at 08:19