tags:

views:

91

answers:

1

Hello

I managed to get a checkboxlist working, and I can't somehow get the values back when I post the "usual way". Must I use Request.Form and loop them into the model before updating the database?

"userobj" has it's userobj.UsersUsergroups count = 0 on post.

control:

        [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UserEdit([BindAttribute(Include = "Firstname,Surname,Username,Email,Password,UserID,UsergroupID")]User userobj)
    {
        if (ModelState.IsValid)
        {
            try
            {
                us.SaveUser(userobj);
            }
            catch
            {
                ModelState.AddModelError("SomeError", "errrrrrrrrror");
            }
        }

        return View("UserDetail", new UsersAdminModel { User = userobj });
    }

And I have checkboxes generated as:

<input id="UsergroupID_0" name="UsergroupID" type="checkbox" value="2" /> <label for="UsergroupID_0" id="label-0">Grupp 1</label><br />
<input id="UsergroupID_1" name="UsergroupID" type="checkbox" value="3" /> <label for="UsergroupID_1" id="label-1">Grupp 2</label><br />
<input id="UsergroupID_2" name="UsergroupID" type="checkbox" value="4" /> <label for="UsergroupID_2" id="label-2">234234</label><br />
<input id="UsergroupID_3" name="UsergroupID" type="checkbox" value="5" /> <label for="UsergroupID_3" id="label-3">234234</label><br />
<input id="UsergroupID_4" name="UsergroupID" type="checkbox" value="6" /> <label for="UsergroupID_4" id="label-4">234234</label><br />
<input id="UsergroupID_5" name="UsergroupID" type="checkbox" value="7" /> <label for="UsergroupID_5" id="label-5">345345345</label><br />
<input id="UsergroupID_6" name="UsergroupID" type="checkbox" value="8" /> <label for="UsergroupID_6" id="label-6">3453453456</label><br />
<input id="UsergroupID_7" name="UsergroupID" type="checkbox" value="9" /> <label for="UsergroupID_7" id="label-7">Grupp 122</label><br />

How can I "populate" the User-object with the checked values after post?

Thanks in advance /M

EDIT:

I tried with:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult UserEdit([BindAttribute(Include = "Firstname,Surname,Username,Email,Password,UserID,UsergroupID")]User userobj, IList UsergroupID)

and it seems to assign the values from the checkboxes to that list...

But what shall I do to assign it to userobj.UsersUsergroups? wrong types when I try

/M

+3  A: 

Check out Phil Haack's excellent blog post on how to model bind a list of elements.

Whether you'll be able to use this directly or not, I'm not sure, but you could always get the ids in a separate parameter and then retrieve the groups by id to add to your user object. I don't know that it will populate the groups from the database just given the ids. A custom model binder may be needed for that.

<input type="hidden" name="UserGroupID.Index" value="0" />
<input type="checkbox" name="UserGroupID[0]" id="UserGroupID_0" value="0" />
    <label for="UsergroupID_0" id="label-0">Group 0</label><br />
 <input type="checkbox" name="UserGroupID[1]" id="UserGroupID_1" value="1" />
    <label for="UsergroupID_1" id="label-1">Group 1</label><br />
 ...
tvanfosson
I use this approach for both TextBox and CheckBox. You only need the hidden index field if you care about the order and if it is there it MUST be in order as 0,1,2,5,6 - 5,6 will be missing from the Array/List when it is created.
Nick Clarke