MVC3 using CheckBox with a complex viewmodel

2019-02-11 11:41发布

问题:

Right guys. I need your brains as I can't find a way to do this properly.

I have a view model:

public class EditUserViewModel
{
    public User User;
    public IQueryable<ServiceLicense> ServiceLicenses;
}

User is unimportant as I know how to deal with it.

ServiceLicenses has the following implementation:

public class ServiceLicense
{
    public Guid ServiceId { get; set; }
    public string ServiceName { get; set; }
    public bool GotLic { get; set; }
}

Getting a checked list of users is cool. It works like a charm.

<fieldset>
    <legend>Licenses</legend>
    @foreach (var service in Model.ServiceLicenses)
    {     
    <p>
        @Html.CheckBoxFor(x => service.GotLic)
        @service.ServiceName
    </p>
    } 
</fieldset>

The problem I'm having is getting the updated ServiceLicenses object with new checked services back to the HttpPost in my controller. For simplicity lets say it looks like this:

    [HttpPost]
    public ActionResult EditUser(Guid id, FormCollection collection)
    {

        var userModel = new EditUserViewModel(id);
        if (TryUpdateModel(userModel))
        {
            //This is fine and I know what to do with this
            var editUser = userModel.User;

            //This does not update
            var serviceLicenses = userModel.ServiceLicenses;

            return RedirectToAction("Details", new { id = editUser.ClientId });
        }
        else
        {
            return View(userModel);
        }
    }

I know I am using CheckBox the wrong way. What do I need to change to get serviceLicenses to update with the boxes checked in the form?

回答1:

i understand that ServiceLicenses property is a collection and you want MVC binder to bind it to you action parameters property. for that you should have indices attached with inputs in your view e.g

<input type="checkbox" name = "ServiceLicenses[0].GotLic" value="true"/>
<input type="checkbox" name = "ServiceLicenses[1].GotLic" value="true"/>
<input type="checkbox" name = "ServiceLicenses[2].GotLic" value="true"/>

Prefix may not be mandatory but it is very handy when binding collection property of action method parameter. for that purpose i would suggest using for loop instead of foreach and using Html.CheckBox helper instead of Html.CheckBoxFor

<fieldset>
    <legend>Licenses</legend>
    @for (int i=0;i<Model.ServiceLicenses.Count;i++)
    {     
    <p>
        @Html.CheckBox("ServiceLicenses["+i+"].GotLic",ServiceLicenses[i].GotLic)
        @Html.CheckBox("ServiceLicenses["+i+"].ServiceName",ServiceLicenses[i].ServiceName)//you would want to bind name of service in case model is invalid you can pass on same model to view
        @service.ServiceName
    </p>
    } 
</fieldset>

Not using strongly typed helper is just a personal preference here. if you do not want to index your inputs like this you can also have a look at this great post by steve senderson

Edit: i have blogged about creating master detail form on asp.net mvc3 which is relevant in case of list binding as well.