MVC binding list of values to checkboxlist in an efficient way I have this code which generates and shows list of selectable items that user can post to controller in order to save into database.

Model looks like this:

public class DeliveryAddressSelectionModel()
{
public List&lt;DeliveryAddress&gt; DeliveryAddresses { get; set; } 
public List&lt;int&gt; SelectedAddressIDs { get; set; }   
}
public class DeliveryAddress()
{
public int ID { get; set; }
public string Location { get; set; }
}


Controller looks like

[HttpGet]
public ActionResult Edit()
{
    var model = new DeliveryAddressSelectionModel();  
    model.DeliveryAddresses = AddressDetails.GetAvailable(); //Get from  EF
    model.SelectedAddressIDs = new List&lt;int&gt;(); //Populate this list with Address IDs for pre-selection     
    return View(model);
}

[HttpPost]
public ActionResult Edit(DeliveryAddressSelectionModel model)
{
    //save the model.SelectedAddressIDs in DB
    return RedirectToAction("Index");
}


And finally view looks like this:

&lt;form method="post"&gt;
    &lt;ul&gt;
        @foreach(var address in DeliveryAddresses)
        {
        &lt;li&gt;
            &lt;input id="address@(address.ID)" type="checkbox" 
                name="SelectedAddressIDs"
                value="@address.ID"
                @(Model.SelectedAddressIDs.Contains(address.ID) ? "checked" : "")
                /&gt;                  
            &lt;label for="address@(address.ID)"&gt;@address.Location&lt;/label&gt;
        &lt;/li&gt;       
        }
    &lt;ul&gt;
    &lt;input type="submit" value="Save Address"/&gt;
&lt;/form&gt;


This code works, but as I am learning  MVC, and I would like to have suggestions. What areas in this code could have been done better, or what is best practice in regards with this kind of model population and posting? Could I use a @Html.CheckBoxFor() helper or this is ok?
In my opinion, your view is doing too much work. This is happening because your model is missing a property.

If you add an IsSelected property to your model, I think we can simplify the view a bit.

public class DeliveryAddress()
{
    public int ID { get; set; }
    public string Location { get; set; }
    public bool IsSelected { get; set; }
}


Now this single model contains all the information you need to bind to the view. You no longer even need the DeliveryAddressSelectionModel. The controller would look something like this (untested, I'm typing in the browser):

[HttpGet]
public ActionResult Edit()
{
    var selectedAddressIDs = new List&lt;int&gt;() { 1, 2, 4 };

    List&lt;DeliveryAddress&gt; model = AddressDetails.GetAvailable();

    foreach(var address in model.Where(a =&gt; selectedAddressIDs.Contains(a.ID)))
    {
        address.IsSelected = true;
    }

    return View(model);
}


Create a partial view for your DeliveryAddress.

@Model DeliveryAddress

@Html.CheckBoxFor(m =&gt; m.IsSelected)
@Html.LabelFor(m =&gt; m. Location)


Then, from your main view, render the partial view.

@Model System.Collections.Generic.List&lt;DeliveryAddress&gt;

&lt;form method="post"&gt;
    &lt;ul&gt;
        @foreach(var address in Model)
        {
          &lt;li&gt;
            @Html.RenderPartial("DeliveryAddress", address)    
          &lt;li /&gt;
        }
    &lt;ul /&gt;
    &lt;input type="submit" value="Save Address"/&gt;
&lt;/form&gt;