Pass entire object from view to controller in ASP.

2020-04-11 18:30发布

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.

3条回答
老娘就宠你
2楼-- · 2020-04-11 18:45

I have searched myself and the best way, aside from passing the ID, that I have found is to store any other variables that you might need into hidden input fields or HTML5 tags. Then you can script a way to handle any button/link click events. This way you can store vital object properties of each record and easily pass them back to a controller. Think Client-side here, Once the data ends up Client-Side, use Client-Side tools to handle and pass it back to server side/controller.

I do something similar with a type of library reservation system that allows users to reserve items on available dates. I pass all available records to the view. Each record has a few fields that I want to hold onto including the ID for users reference. When the user clicks the button, I collect the needed fields.

You could use HTML5 form input fields that are hidden or you could just use JavaScript to collect those values using GetElementByID. An example of this would be to store the ID in the div wrapper. Then have another div hold a sub parameter. You can use Javascript to find the record ID and then get the second div by it's id. Example would be get the id NameRecord from XRecord where X = the ID passed.

I then pass those values to the controller, instantiate a new class/object for the reservation. The new class object also has the item class/object as a property. For example consider the following;

    var reservation = new Reservation
    {
    myKit = new ResourceKit()
    };

After that, you can store it in a session if you need to build on it. In my case I am holding it in a session because I allow the user to check availability/dates. These items are a physical resources that gets checked out similar to a library and are transferred via office mail.

If you dont mind the data sitting client-side, you can store it using LocalStorage and JavaScript. This type of data isnt secure at all much like a cookie. One of the ways that I have used this is to set site preferences. Users can select a color scheme and those preferences are stored in LocalStorage. That way when the return to the site those preferences remain. This is a key attribute of LocalStorage and might not be applicable to your needs/circumstances.

查看更多
何必那么认真
3楼-- · 2020-04-11 18:49

Assuming that your Update View isn't of type IEnumerable...

You just need to pass the ID of the record that you want to send to the Update view...

Like so:

@Html.Action("Update me!", "Update", new { id = item.ID })

Then your Update action would look like this:

[HttpGet]
public ActionResult Update(int id)
{
    var parameter = db/* connection string variable */.TableName.Find(id);
    return View(parameter);
}

Then your link should work appropriately.

Hope this helps!

查看更多
Melony?
4楼-- · 2020-04-11 18:57

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.

查看更多
登录 后发表回答