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.
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:
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.Then, in your view:
And finally, change your action method signature:
Then, you can easily get the list of selected ids inside the action with: