1

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 Answer 1

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)
4
  • I changed my Models to encorproate your answer, they list fine ( however unchecked) but somehow Object reference not set to an instance of an object. is showing up on the line of @Model.Prospects[i].Name ideas? - will test the return Sep 18, 2013 at 20:45
  • Name is just a property I made up to signify the property that holds the text for what you want displayed next to the checkbox. Sep 18, 2013 at 21:19
  • Absolutely Awesome, sorry it took me a while to wrap around my head what you were doing. Also is there no way to do this with Ienumerables? you must do list? Sep 19, 2013 at 15:04
  • No, it has to be a list. You have to use a standard for loop (rather than foreach) to get Razor to generate the field names properly, which means you need to specify the end of the loop, which requires a count. With an IEnumerable that will use up your enumeration and then you can't actually run the loop. Sep 19, 2013 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.