post viewmodel with IList to controller post metho

2019-06-10 07:38发布

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

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
}

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?

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

Thanks

2条回答
唯我独甜
2楼-- · 2019-06-10 07:59

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>

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.

Hope this helps.

查看更多
放我归山
3楼-- · 2019-06-10 08:06

For anything but simple CRUD apps, it's bad form to accept view models for [HttpPost] methods. ViewModels are for viewing, not for posting.

Instead, try:

[HttpPost]
public ActionResult SubmitMaintenance(IList<MyMaintenance> myMaintenanceList)
{
    //Validate and then send to database
}
查看更多
登录 后发表回答