Reputation: 727
I have a method that passes back my viewmodel from a view as such in my post:
[HttpPost]
public ActionResult DoStuff(daViewModel model)
{
string whatever = model.Name;
int id = model.Id;
return View();
}
What type of object get's passed back to my controller method on the post (is my viewmodel wrapped in an httppost type of class?) Is there a generic/type that I can pass such as:
[HttpPost]
public ActionResult DownloadFiles(object model)
{
// cast my daViewModel from object model as passed in???
string whatever = model.Name;
int id = model.Id;
return View();
}
Upvotes: 0
Views: 2514
Reputation: 33071
You can pass a FormCollection
object:
[HttpPost]
public ActionResult DownloadFiles(FormCollection collection)
{
// if you want to extract properties directly:
string whatever = collection["Name"];
int id = int.Parse(collection["Id"]);
// if you want to convert the collection to your model:
SomeModel model;
TryUpdateModel(model, collection);
return View();
}
The TryUpdateModel
method returns a boolean. If it succeeds in updating the model it will return true, otherwise it will return false. The form values being passed in should match the property names of your model.
If you're asking what model gets passed back when you call return View()
then the answer to that is nothing unless you tell it to. There is an overload on the View()
method which takes in a model:
return View(model);
You should be returning the type that the View expects to see. If you defined your view as having a model of Foo
then you better return a Foo
in your controller.
Upvotes: 3