开发者

ASP.NET MVC Model With Dynamic Number of CheckBoxes

I have a database with a one-to-many relationship that I'm having a difficult time modeling in ASP.NET MVC. For the sake of simplicity, let's say table A represents office buildings, table B represents employees and table C creates the one-to-many relation between employees and buildings to indicate which employees have access to a particular building.

Employee
EmployeeId - int
FirstName - string
LastName - string

Office
OfficeId - int
Location - string

EmployeeOffice
EmployeeId - int
OfficeId - int

When new employees come on board, I'd like to assign them to any office buildings they would be able to access. To do this, the UI calls for check boxes for each individual office building. Checking the boxes grants the user access to the related office building.

[ ] - Office 1
[ ] - Office 2
[ ] - Office 3
[ ] - Office 4

My concern is, offices开发者_JS百科 should be dynamic. In other words, an office (or offices) can come or go at any time.

The model is actually more complicated than what I have depicted. As such, I have a CreateEmployeeViewModel, which contains properties (with annotations) as follows:

public class CreateEmployeeViewModel
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public IDictionary Offices
    {
        get;
        private set;
    }


    public CreateEmployeeViewModel(IDictionary offices)
    {
        Offices = offices;
    }
}

My view resembles the following

<div>
    <%: Html.LabelFor(model => model.FirstName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.FirstName) %>
    <%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>

<div>
    <%: Html.LabelFor(model => model.LastName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.LastName) %>
    <%: Html.ValidationMessageFor(model => model.LastName) %>
</div>

<!-- This is really ugly, so I welcome any suggested updates -->
<% foreach (KeyValuePair<int, string> office in Model.Offices) { %>
    <label for="Office<%: office.Key %>"><%: office.Value %></label>
    <input id="Office<%: office.Key %>" type="checkbox" value="<%: office.Key %>" />
<% } %>

When I click the submit button for the form, I expect to get back the strongly typed view model, in addition to the check boxes so that I know which offices to assign users to. I created my action method like the following:

public ActionResult Create(CreateUserViewModel user, FormCollection collection)

The reason for the additional FormCollection object is I was hoping I could get form values in addition to the ones found on the view model (e.g., the check boxes). Unfortunately, however, the collection contains only information for the properties found in my view model.

What is the right way to handle forms with this design in ASP.NET MVC 2.0?


You need to add the "name" attribute to your checkbox input elements - otherwise they will not show up in the FormCollection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜