View not passing Ienumerable of Model

2019-06-07 15:07发布

问题:

Edit: Removed Partial View to make things simpler. Now I just need to find out why The View isn't Posting the Values

  • ViewModelProspectUsers

    public class ViewModelProspectUsers
    {
    
        public int Id { get; set; }
        public string User { get; set; }
        public IEnumerable<ViewModelProspectSelect> Prospects { get; set; }
    
    }
    
  • ViewModelProspectSelect

    public class ViewModelProspectSelect
    {
    
        public int ProspectID { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    
    }
    
  • View

    @model OG.ModelView.ViewModelProspectUsers
    
    
    @using (Html.BeginForm())
    {
    
        @Html.HiddenFor(model => model.Id)
    
        <h5>Please Select Prospects you wish to assign to this User.</h5>
    

-----HERE is where the partial used to be, these values aren't being posted to the [Post] Method------

-----------------------------------------However they are populating just fine----------------------------------------

        @foreach (var item in Model.Prospects)
        {
            @Html.HiddenFor(x => item.ProspectID)


                @Html.DisplayFor(x => item.Name)

                @Html.EditorFor(x => item.IsSelected)

        }

        @*@Html.Partial("_ShowProspectCheckedForUser", Model.Prospects)*@
        @*@Html.Partial("_ShowProspectCheckedForuser", new OG.ModelView.ViewModelProspectSelect())*@


    <input type="submit" value="Save changes" />
    @Html.ActionLink("Cancel", "Index")

}
  • Post

     [HttpPost]
     public ActionResult UsersInProspect(ViewModelProspectUsers viewModel)   
    

If i were to look at viewModel.Prospects(m=>m.isSelected) //<- this value is Null shouldn't be

My viewmodel Variableis showing Data but not for the Ienumerable.

回答1:

When dealing with list-type objects, you must reference them with array notation to have the field names generated in a way that the modelbinder can parse them back, i.e:

for (var i = 0; i < Model.Count(); i++)
{
    @Html.LabelFor(m => Model[i].SomeProperty)
    @Html.EditorFor(m => Model[i].SomeProperty)
}

In your scenario, you'd be better served by using a view model to contain your list and adding a Selected property to the items so that you can track which ones were or were not selected.

public class ViewModelProspects
{
    public List<ViewModelProspectSelect> Prospects { get; set; }
}

public class ViewModelProspectSelect
{
    // Whatever else you have

    public bool Selected { get; set; }
}

Then, in your view:

@model ViewModelProspects

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.Prospects.Count(); i++)
    {
        <label>
            @Html.HiddenFor(m => Model.Prospects[i].Id)
            @Html.CheckboxFor(m => Model.Prospects[i].Selected, true)
            @Model.Prospects[i].Name
        </label>
    }
}

And finally, change your action method signature:

[HttpPost]
public ActionResult UsersInProspect(ViewModelProspects model)

Then, you can easily get the list of selected ids inside the action with:

var selectedIds = model.Prospects.Where(m => m.Selected).Select(m => m.Id)