2009-12-22 81 views
0

我有一个mvc应用程序,我想将用户分配给配置文件。我有两个多行选择列表(即< select size =“10”> </select>)。一个用于用户所属的配置文件,另一个用于用户可以成为其一部分的可用配置文件。分配用户到配置文件

我正在使用JavaScript和两个按钮来移动这两个列表之间的项目。当表单提交时,Request.Form对象只知道在任一列表中选择的选项,但我想知道'用户属于'列表中'用户配置文件'中的所有项目。

我能做些什么来实现这个目标?我正在考虑使用隐藏字段,并将这些值移到字段中,当这些字段被移动到'用户属于'列表中的配置文件'时。 (即< input type =“hidden”name =“user_profiles”values =“3,8,12”>)。

您认为如何?有一个更好的方法吗?谢谢。

回答

1

您可以通过脚本来选择提交事件列表中的所有内容。

+1

从未想过这一点,谢谢。 – modernzombie 2009-12-22 14:27:08

0

将FormCollection传递给该操作。然后更新模型并存储数据。

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SaveProfile(FormCollection collection){ 

    // collection is a key based collection so pass the key or the control name to get 
    // its value 

    Profile p = new Profile(); 
    p.UserId = collection["UserId"]; 
    p.Name = collection["Name"]; 
    p.Birthdate = collection["Birthdate"]; 
    p.ProfileOption = collection["ProfileOption"]; 

    // DB logic should be done in a service but this is a very simplified example 
    profileRepository.Update(p); 

    return View(); 
} 
+0

@gmcalab,这提供了与Request.Form [“key”]相同的结果,它只给出列表中的选定选项,而不是列表中的所有选项(不管它们是否被选中)。 – modernzombie 2009-12-22 14:38:23