如何通过IEnumerable的模型Html.actionLink参数 [英] How to pass IEnumerable Model in parameter of Html.actionLink

查看:297
本文介绍了如何通过IEnumerable的模型Html.actionLink参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我要救我的模型的所有值。

I have a question. I need to save all values of my model.

在index.cshtml,我有:

In index.cshtml, I have :

@model IEnumerable的

@model IEnumerable

我可以在我看来,我的foreach中的所有值。

I can get all my values in foreach in my view.

@foreach(以型号VAR项){
  item.ID,item.Name
  }

@foreach (var item in Model) { item.ID, item.Name }

我需要我的整个模式传递给我的控制器有一个链接:

html.actionlink(保存吗?,保存,...);

html.actionlink("Save It", Save, ...);

我怎么能这样做吗?

感谢

推荐答案

您不能用这样的GET请求传递一个整个模型。你可以使用HTML表单:

You cannot pass an entire model with a GET request like this. You could use an HTML form:

@using (Html.BeginForm("Save", "SomeController"))
{
    @Html.EditorForModel()
    <input type="submit" value="Save It">
}

在这里您已经定义了一个模板编辑器,这种模式(〜/查看/共享/ EditorTemplates / Item.cshtml ),使用隐藏域:

@model Item
@Html.HiddenFor(x => x.ID)
@Html.HiddenFor(x => x.Name)

这部分的名称和位置是非常重要的。它应位于〜/查看/共享/ EditorTemplates 和文件名应该叫 Item.cshtml 如果项目是项目&GT;模型集合,即的IEnumerable&LT的类型名称。编辑器模板将对于集合的每个项目执行,并呈现对应隐藏字段,这将允许其输送到服务器

The name and location of this partial is important. It should be located in ~/Views/Shared/EditorTemplates and the filename should be called Item.cshtml if Item is the type name of your model collection i.e. IEnumerable<Item>. The editor template will be executed for each item of the collection and render the corresponding hidden fields that will allow to transport it to the server.

这个表格将发送成功的项目收集到下列控制器动作:

This form will successfully send the collection of items to the following controller action:

[HttpPost]
public ActionResult(IEnumerable<Item> model)
{
    ...
}


和这里进行的另一种方式。如果用户不应该修改视图的模型值,那么你可以简单地使用一些独特的标识符,让您重新获取来自无论你取它最初模型。因此,例如:


And here's an alternative way to proceed. If the user is not supposed to modify the model values on the view then you could simply use some unique identifier allowing you to refetch the model from wherever you fetched it initially. So for example:

public ActionResult Index(int id)
{
    IEnumerable<Item> model = ... fetch the model using the id
    return View(model);
}

和视图生成一个链接通过这个ID:

and in the view generate a link passing this id:

@Html.ActionLink("Save It", Save, new { id = "123" })

这篇关于如何通过IEnumerable的模型Html.actionLink参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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