1

I have a class that contains a list of another class, however when I try and use the html helper CheckBoxFor I can't use it, and if I use the CheckBox html helper the model refused to pick it up leaving it null.

I'm at a lost how to fix it so any help would be appreciated please?

The root level class:

public class Roles
{
    public Roles()
    {

    }

    public string Role { get; set; }
    public List<Suppliers> Suppliers { get; set; }
    public string SuppliersDelimited { get; set; }
    public List<SuppliersCheck> SupplierListing { get; set; } // The one I need to loop through
}

The sub level class:

public class SuppliersCheck
{
    public SuppliersCheck()
    {

    }

    public string Code { get; set; }
    public string Name { get; set; }
    public bool selected { get; set; }
}

My view code:

@model Project.OrdersExchange.MVC.Models.Roles 

@{
    ViewBag.Title = "Assign Roles";
}

<h2 class="page-header">
    Assign Roles - @Model.Role
    <a href="/Users/SupplierRoles" class="btn btn-default pull-right">
        Back
    </a>
</h2>

@using (Html.BeginForm(Model))
{

@Html.HiddenFor(x => x.Role)
@Html.HiddenFor(x => x.SuppliersDelimited)

<table>
    @foreach(var sup in Model.SupplierListing)
    {
    <tr>
        <td>
            @Html.Label(sup.Name + "(" + sup.Code + ")", new { @class = "col-md-2 control-label" })
        </td>
        <td>
            @Html.CheckBox("selected", new { value = sup.selected, @checked = sup.selected, @class = "form-control" })
            @*@Html.CheckBoxFor(sup.selected, new { @class = "form-control"}) Will not work as the helper is looking for a bool within the role class *@ 
        </td>
    </tr>
    }
</table>

<input type="submit" value="Submit" class="btn btn-default" />
}
killercantom
  • 61
  • 1
  • 6

0 Answers0