且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将整个对象从视图传递到ASP.NET MVC 5中的控制器

更新时间:2023-02-25 19:06:09

您的对象可能太大了!查询字符串对通过基于浏览器的数据可以传递多少数据有限制。您应该考虑传递(记录的)唯一ID值,并使用该ID值在操作方法中从db获取整个记录并将其传递给视图。

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>
}

并采用操作方法

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

假设 GetItemFromId 方法从唯一ID值返回方法/视图模型。基本上,您可以使用此唯一ID从数据库表/存储库中获取整个记录。

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.