0

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

steveareeno
  • 1,925
  • 5
  • 39
  • 59

2 Answers2

1

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.

erdal
  • 421
  • 3
  • 10
0

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
}
User
  • 1,118
  • 7
  • 23
  • So how do I get the changed values in the view (populated with a viewmodel) when posting if not the viewmodel? – steveareeno Aug 13 '13 at 23:01
  • Between this and Erdal's recommendation I got it working. Could have swore I tried putting the list as a parameter in my controller, but maybe not. Thanks! BTW, I am still interested in how to pass the viewmodels data back to the post method. I am just learning MVC and want to follow good practices – steveareeno Aug 13 '13 at 23:05
  • 2
    So I was trying to find out why it is bad format to pass the VM to the post method but couldn't find anything. I did find a lot of examples of doing exactly that, like this from Darin Dimitrov, who seems to answer a lot of MVC posts on stackoverflow: http://stackoverflow.com/questions/9891040/asp-net-mvc-3-viewmodel-data-annotations. If for some reason it is not advisable to do this, could someone tell me the alternative? Thanks! – steveareeno Aug 14 '13 at 13:13