2016-05-16 16 views
0

私はこの問題を解決する方法についていくつかの助けが必要です。私はいくつかのことを試してきましたが、私はどこにもいません。 ここに私の問題があります:dropdownlist MVC 5のEFで作成したテーブルを使用します。dropdownlistには、ユーザーによって作成されたContactGroupsが含まれています。このdropdownlistは、私のデータベースの一意の値によって変わります。 dropdownlistContactGroupsの中から選択すると、選択したContactGroupに属する連絡先のみが表に表示されます。たとえば、dropdownlistの「Friends」をクリックすると、テーブルにContactGroupという値の連絡先のみが「Friends」として表示されます。DropDownListをフィルタテーブルMVCに5

私の質問:これを実装する最良の方法は何ですか? DropDownListで選択した値を取得するにはどうすればよいですか?この値をコントローラーに戻して連絡先をフィルターに掛けることがベストプラクティスですか?部分ビューを使用すべきですか?

ビュー

@model PagedList.IPagedList<Webassistent.Models.ContactModel> 
 
@using PagedList.Mvc; 
 

 
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> 
 

 
@{ 
 
    ViewBag.Title = "Contacts"; 
 
} 
 

 
<h2>Contacts</h2> 
 

 
@Html.DropDownList("CG", 
 
(@ViewBag.Groups) as IEnumerable<SelectListItem>, "Select Group", 
 
new { htmlAttributes = new { @class = "form-control" } }) 
 

 

 
<p> 
 
    @Html.ActionLink("Create New", "Create") 
 
</p> 
 

 
@using (Html.BeginForm("Index", "Contact", FormMethod.Get)) { 
 
    <p> 
 
    Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
 
    <input type="submit" value="Search" /> 
 
    </p> 
 
} 
 
<table class="table"> 
 
    <tr> 
 
     <th> 
 
     First Name 
 
     </th> 
 
     <th> 
 
     @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter }) 
 
     </th> 
 
     <th> 
 
     Private Phone 
 
     </th> 
 
     <th> 
 
     Private Email 
 
     </th> 
 
     <th> 
 
     @Html.ActionLink("ContactGroup", "Index", new { sortOrder = ViewBag.GroupSortParm, currentFilter = ViewBag.CurrentFilter }) 
 
     </th> 
 
     <th></th> 
 
    </tr> 
 

 
    @foreach (var item in Model) { 
 
    <tr> 
 
     <td> 
 
     @Html.DisplayFor(modelItem => item.FirstName) 
 
     </td> 
 
     <td> 
 
     @Html.DisplayFor(modelItem => item.LastName) 
 
     </td> 
 
     <td> 
 
     @Html.DisplayFor(modelItem => item.PrivatePhone) 
 
     </td> 
 
     <td> 
 
     @Html.DisplayFor(modelItem => item.PrivateEmail) 
 
     </td> 
 
     <td> 
 
     @Html.DisplayFor(modelItem => item.ContactGroup) 
 
     </td> 
 
     <td> 
 
     @Html.ActionLink("Edit", "Edit", new { id=item.ContactId }) | @Html.ActionLink("Details", "Details", new { id=item.ContactId }) | @Html.ActionLink("Delete", "Delete", new { id=item.ContactId }) 
 
     </td> 
 
    </tr> 
 

 
</table> 
 

 
<br />Page @(Model.PageCount 
 
< Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page=>Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter}))

コントローラ

[Authorize] 
    public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page) 
    { 
     ViewBag.CurrentSort = sortOrder; 
     ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; 
     ViewBag.GroupSortParm = sortOrder == "Group" ? "group_desc" : "Group"; 

     if (searchString != null) 
     { 
      page = 1; 
     } 
     else 
     { 
      searchString = currentFilter; 
     } 
     ViewBag.CurrentFilter = searchString; 

     string thisUser = User.Identity.GetUserId(); 
     var contacts = from s in db.ContactModels where s.UserId == thisUser select s; 

     if (!String.IsNullOrEmpty(searchString)) 
     { 
      contacts = contacts.Where(s => s.LastName.Contains(searchString) || s.FirstName.Contains(searchString) || s.ContactGroup.Contains(searchString)); 
     } 

     switch (sortOrder) 
     { 
      case "name_desc": 
       contacts = contacts.OrderByDescending(s => s.LastName); 
       break; 
      case "Group": 
       contacts = contacts.OrderBy(s => s.ContactGroup); 
       break; 
      case "group_desc": 
       contacts = contacts.OrderByDescending(s => s.ContactGroup); 
       break; 
      default: 
       contacts = contacts.OrderBy(s => s.LastName); 
       break; 
     } 
     int pageSize = 3; 
     int pageNumber = (page ?? 1); 

     var groups = contacts.Where(x => x.ContactGroup !=null).Select(x => x.ContactGroup).Distinct(); 
     ViewBag.Groups = new SelectList(groups); 

     return View(contacts.ToPagedList(pageNumber, pageSize)); 
    } 

私は、現在のユーザーに属するContactGroup Sを追跡するためにViewBag.Groupsを使用すると考えていました。これはDropDownListによって表示され、ContactGroupを表示するために使用されます。

私はこれを行う簡単な方法があると確信していますが、解決策が見えないようです。どんな助けも大歓迎です!!

答えて

0
  1. #3のプロパティを持つコントローラから返されたビューのビューモデル。
  2. Views \ SharedフォルダにあるHTML Helperです。
  3. HTMLヘルパーのプロパティを定義するオブジェクト。
  4. HTMLヘルパーを定義するコントローラの作成オブジェクトを作成します。
関連する問題