将整个对象从视图传递到ASP.NET MVC 5中的控制器 [英] Pass entire object from view to controller in ASP.NET MVC 5

查看:86
本文介绍了将整个对象从视图传递到ASP.NET MVC 5中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将整个对象从ASP.NET MVC 5 View传递到控制器?这是我的情况:

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


  • 我有一个视图,显示数据库表中的所有行

  • 该视图的模型为IEnumerable

  • 每行数据后都有一个链接,可链接到支架式UPDATE视图

是否有一种方法可以将整个对象传递给Update控制器方法,以便它首先用旧数据填充表单输入?像这样的东西:

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

然后在控制器中

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.

推荐答案

您的对象可能太大了!查询字符串对通过基于浏览器的数据可以传递多少数据有限制。您应该考虑传递(记录的)唯一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.

这篇关于将整个对象从视图传递到ASP.NET MVC 5中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆