使用IList将viewmodel发布到控制器post方法 [英] post viewmodel with IList to controller post method

查看:66
本文介绍了使用IList将viewmodel发布到控制器post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有IList的视图模型:

I have a view model with a IList:

public class MyMaintenanceListViewModel
{
    public IList<MyMaintenance> MyMaintenanceList { get; set; }

    [Display(Name = "Network User Name:")]
    public string NetworkUserName { get; set; }

    [Display(Name = "Password:")]
    public string Password { get; set; }
}

我有一个视图,其模型设置为viewmodel:

I have a view whose model is set to the viewmodel:

@model EMMS.ViewModels.MyMaintenanceListViewModel

@using (Html.BeginForm("SubmitMaintenance", "Maintenance"))
{
    <table id="searchtable" class="MyMaintenance">
        <tr>
            <th style="width: 50px; text-align: left;">Id</th>
            <th style="width: 200px; text-align: left;">Equipment Id</th>
            <th style="width: 100px; text-align: left;">Task Id</th>
            <th style="width: 150px; text-align: left;">Date Completed</th>
            <th style="width: 100px; text-align: left;">Elapsed Time</th>
            <th style="width: 200px; text-align: left;">Created</th>
            <th style="width: 50px;"></th>
        </tr>
    @for (int i = 0; i < Model.MyMaintenanceList.Count; i++)
    {
        var item = Model.MyMaintenanceList[i];
       <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RowId)
                @Html.HiddenFor(modelItem => item.RowId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EquipmentId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TaskId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateCompleted)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ElapsedTimeMinutes)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreateDate)
            </td>
        </tr>
    }
    </table>
}

我的控制器看起来像这样:

My controller looks something like this:

[HttpPost]
public ActionResult SubmitMaintenance(MyMaintenanceListViewModel myMaintenanceListViewModel)
{
    // do something with IList "MyMaintenanceList" in myMaintenanceListViewModel
}

但是,当我在上面的控制器post方法上断点并提交表单时,即使视图中有项目,myMaintenanceListViewModel中的MyMaintenanceList列表也会显示count = 0.如何将此表中的项目传递给控制器​​中的post方法?

However, when I breakpoint the controller post method above and submit the form, the MyMaintenanceList list in myMaintenanceListViewModel says count=0, even though there are items in the view. How can I pass the items in this table to a post method in my controller?

我正在尝试遍历控制器中MyMaintenanceList列表中的项目.希望这是有道理的.

I am trying to iterate over the items in the MyMaintenanceList list in the controller. Hope this makes sense.

谢谢

推荐答案

MVC模型绑定使用输入元素的name属性将表单数据绑定到模型. 首先,您不应该在for循环中创建项目变量.您应该这样绑定数据:

MVC model binding uses your input elements' name attribute to bind your form data to your model. First of all, you shouldn't create item varible in the for loop. You should bind data like that:

    <tr>
            <td>
                @Html.DisplayFor(modelItem => Model.MyMaintenanceList[i].RowId)
                @Html.HiddenFor(modelItem => Model.MyMaintenanceList[i].RowId)
            </td>
   </tr>

第二,如果将数据发布到服务器,则应使用输入类型元素.因此,如果要将数据发布到RowId旁边的服务器,则必须将 Html.HiddenFor 用于MyMaintenanceList的其他属性.

Secondly, if you post data to server, you should use input type elements. So if you want to post data to the server beside RowId, you must use Html.HiddenFor for other properties of MyMaintenanceList.

希望这会有所帮助.

这篇关于使用IList将viewmodel发布到控制器post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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