Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 909
dzenesiz Avatar asked Jan 06 '23 11:01

dzenesiz


1 Answers

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)
{
  <tr>
    <td> @Html.Action("Update me!", "Update", new {  id = item.Id }) </td>
  </tr>
}

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.

like image 162
Shyju Avatar answered Jan 14 '23 00:01

Shyju