dzenesiz dzenesiz - 1 month ago 11x
C# Question

Pass entire object from view to controller in ASP.NET MVC 5

Is there a way to pass entire object from ASP.NET MVC 5 View to a Controller? This is my situation:

  • I have a View that displays all rows from a DB table

  • The view's model is IEnumerable

  • Each row has a link after it's data that leads to the scaffolded UPDATE view

Is there a way to pass the entire object to the Update controller method so it would initially fill the form inputs with the old data? Something like:

@Html.Action("Update me!", "Update", new { objectFromModelList })

And then in the controller

public ActionResult Update(MyType parameter)
return View(parameter);

Or something like that. Please help, I am new to this and can't find the answer anywhere.


Your objects could be so big! Query string's has a limitation on how much data you can pass via those based on the browser. You should consider passing a unique id value (of the record) and using which get the entire record from db in your action method and pass that to the view.

@foreach(var item in SomeCollection)
    <td> @Html.Action("Update me!", "Update", new {  id = item.Id }) </td>

and in the action method

public ActionResult Update(int id)
    var item = GetItemFromId(id);
    return View(item);

Assuming GetItemFromId method returns the method/view model from the unique id value. Basically you get the entire record using this unique id from your db table/repository.