问题描述
有没有办法将整个对象从 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:
- 我有一个显示数据库表中所有行的视图
- 视图的模型是 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 值(记录的)并使用 which 在您的操作方法中从 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 中将整个对象从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!