如何使用asp.net mvc和jquery实现带有“添加/删除”按钮的列表框

时间:2022-01-19 12:57:18

Currently i have the following partial view which list all the available users and a "Add User to Class" link beside each user to register a user in a class:-

目前,我有以下局部视图,列出所有可用用户,并在每个用户旁边添加“添加用户到班级”链接,以便在班级中注册用户: -

@model IEnumerable<Elearning.Models.User>
<tr>
    <th>
        Student ID
    </th>
</tr>

@foreach (var item in Model) {
    <tr >
        <td>
            @Html.DisplayFor(modelItem => item.UserID)
        </td>
        <td id = "userclass">
              @Ajax.ActionLink("Add User to Class", "Register", "User", new { id = item.UserID, classid = ViewBag.id },
                  new AjaxOptions { 
                      InsertionMode = InsertionMode.InsertAfter,
                      HttpMethod = "POST",
                      UpdateTargetId = "incrementadd"
               })
         </td>
    </tr>
}

The action method which will be called when clicking on the "Add User to Class" link is:-

单击“将用户添加到类”链接时将调用的操作方法是: -

[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Register(string id, int classid)
{
    try
    {
       //Update code here
       User user = r.FindUser(id);
       Users_Classes uc = new Users_Classes();
       uc.AddedDate = DateTime.Now;
       uc.ClassID = classid;
       user.Users_Classes.Add(uc);
       r.Save();
       ViewBag.classid = classid;
       return PartialView("_usersearch2", uc);
    }
}

The above code is working fine , but i find it not easy to add each user by clicking on the link. So i am trying to add a listbox with a add/remove buttons to register and unregister user based on user selection, but i can not figure out how i can modify my code to implement the listbox with "Add/Remove" button using Jquery to make my system more user friendly. can anyone refer me to helping materials or sample code which can help me in my development? Best Regards

上面的代码工作正常,但我发现通过点击链接添加每个用户并不容易。所以我试图添加一个带有添加/删除按钮的列表框来根据用户选择注册和取消注册用户,但我无法弄清楚如何使用Jquery来修改我的代码以使用“添加/删除”按钮实现列表框让我的系统更加用户友好。任何人都可以推荐我帮助材料或示例代码,这可以帮助我进行开发吗?最好的祝福

1 个解决方案

#1


1  

For simple multi selects I am using this jquery plugin:

对于简单的多选,我使用这个jquery插件:

http://harvesthq.github.com/chosen/

http://harvesthq.github.com/chosen/

Then on your view just add something like this to your form:

然后在您的视图上添加这样的内容到您的表单:

@Html.ListBoxFor(model => model.YourCollectionProperty, MyRepository.FetchPropertySelectList().ToList())

Very straightforward and easy to use.

非常简单易用。

If you need any more help or advice on how to implement let me know.

如果您需要任何有关如何实施的帮助或建议,请告诉我。

#1


1  

For simple multi selects I am using this jquery plugin:

对于简单的多选,我使用这个jquery插件:

http://harvesthq.github.com/chosen/

http://harvesthq.github.com/chosen/

Then on your view just add something like this to your form:

然后在您的视图上添加这样的内容到您的表单:

@Html.ListBoxFor(model => model.YourCollectionProperty, MyRepository.FetchPropertySelectList().ToList())

Very straightforward and easy to use.

非常简单易用。

If you need any more help or advice on how to implement let me know.

如果您需要任何有关如何实施的帮助或建议,请告诉我。