【问题标题】:passing model from view to controller in mvc3 c#在mvc3 c#中将模型从视图传递到控制器
【发布时间】:2012-07-21 21:07:30
【问题描述】:
 @foreach (var item in Model.AllManagementActions)
{
 <tr>
     <td>
         @Html.DisplayFor(modelItem => item.Id)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Name)
     </td>      

我想将所有这些值传递给我的控制器。 ID、姓名等 我不能只传递物品本身吗? 这是我当前的代码,但它不起作用。

<a class="complete" title="My Action" data-url="@Url.Action("Submit", "Period", new { tpId = Model.Id ,it = item})" style="cursor: pointer;">       

如果我传递 item.name、item.id、item.number 等,它会起作用

我可以通过模型吗?

【问题讨论】:

    标签: c# asp.net-mvc-3 view model controller


    【解决方案1】:

    不要在你的视图中迭代 - 使用 editor templates 作为类型。

    有一个“~/shared/EditorTemplates/ManagementAction.ascx”(或.cshtml,如果使用剃须刀)呈现单个ManagementAction。

    在视图中,而不是迭代使用:

    @Html.EditorFor(model => model.AllManagementActions)
    

    【讨论】:

    • 也为了显示一个表格,他可以使用 WebHelper 类中的 WebGrid,只接收您的数据的 IEnumerable 并知道该怎么做。 msdn.microsoft.com/en-us/library/…
    • 如果他创建了编辑器模板,他就能将模型传递给控制器​​?
    • @aquinas - 是的。我自己使用它来获取包含作为单个强类型参数传递给操作的集合的模型。
    • 我猜我以为他希望能够编辑列表中的一个 ManagementAction。也许我错了……
    【解决方案2】:

    是的,您可以传递整个模型,但您应该使用提交按钮而不是锚标记,并将代码放入

    using(Html.BeginForm)
    {
       @foreach (var item in Model.AllManagementActions)
       {
          //your desire.
       }
       <input type="submit" value="Save" />
    }
    

    在您的控制器中,您将在帖子中收到模型

     [HttpPost]
     public ActionResult SaveViewData(YourModel model)
     {
        //your logic.
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多