0

現在、利用可能なすべてのユーザーを一覧表示する次の部分ビューと、各ユーザーの横にある[クラスにユーザーを追加]リンクを使用して、ユーザーをクラスに登録しています。

@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>
}

[クラスにユーザーを追加]リンクをクリックしたときに呼び出されるアクションメソッドは次のとおりです。-

[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);
    }
}

上記のコードは正常に機能していますが、リンクをクリックして各ユーザーを追加するのは簡単ではありません。だから私はユーザーの選択に基づいてユーザーを登録および登録解除するための追加/削除ボタンでリストボックスを追加しようとしていますが、Jqueryを使用して「追加/削除」ボタンでリストボックスを実装するためにコードを変更する方法を理解できません私のシステムをよりユーザーフレンドリーにします。誰かが私の開発に役立つ資料やサンプルコードを紹介してくれますか?よろしくお願いします

4

1 に答える 1

1

単純な複数選択のために、私はこのjqueryプラグインを使用しています:

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

次に、ビューで次のようなものをフォームに追加します。

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

非常に簡単で使いやすいです。

実装方法についてさらにヘルプやアドバイスが必要な場合は、お知らせください。

于 2012-12-21T13:30:10.697 に答える