简体   繁体   English

绑定多个值以在发布时建模

[英]Bind multiple values to model on post

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? 在更新数据库之前,是否必须使用Request.Form并将它们循环到模型中?

"userobj" has it's userobj.UsersUsergroups count = 0 on post. “ userobj”具有userobj.UsersUsergroups在发布时计数= 0。

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 在此先感谢/ M

EDIT: 编辑:

I tried with: 我尝试过:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult UserEdit([BindAttribute(Include = "Firstname,Surname,Username,Email,Password,UserID,UsergroupID")]User userobj, IList UsergroupID) [AcceptVerbs(HttpVerbs.Post)]公共ActionResult UserEdit([BindAttribute(Include =“ Firstname,Surname,Username,Email,Password,UserID,UsergroupID”)]用户userobj,IList用户组ID)

and it seems to assign the values from the checkboxes to that list... 并且似乎将复选框中的值分配给该列表...

But what shall I do to assign it to userobj.UsersUsergroups? 但是我应该怎么做才能将其分配给userobj.UsersUsergroups? wrong types when I try 我尝试的类型错误

/M /米

Check out Phil Haack's excellent blog post on how to model bind a list of elements. 查阅Phil Haack的精彩博客文章 ,了解如何对元素列表进行模型绑定。

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. 不确定您是否可以直接使用它,但是您始终可以在单独的参数中获取ID,然后按ID检索组以添加到您的用户对象中。 I don't know that it will populate the groups from the database just given the ids. 我不知道它会从给定ID的数据库中填充组。 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 />
 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM