DropDownList过滤表MVC 5

时间:2016-05-16 13:21:32

标签: html asp.net-mvc entity-framework razor drop-down-menu

我需要一些帮助才能解决这个问题。我一直在尝试几件事,但我无处可去。 这是我的问题:我想在{1}}中使用我在MVC 5中使用EF创建的表。dropdownlist包含用户创建的dropdownlist。此ContactGroups将根据我的数据库中的唯一值而更改。当我在dropdownlist中的ContactGroups之间进行选择时,我希望该表格仅显示属于所选dropdownlist的联系人。例如:如果我点击"朋友"在ContactGroup中,我希望该表仅显示dropdownlist值为{&#34}的朋友"。

我的问题:实施此方法的最佳方法是什么?如何在ContactGroup中获取所选值?最好将此值发送回控制器并过滤掉联系人吗?我应该使用部分视图吗?

查看



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)); } 来跟踪属于当前用户的ViewBag.GroupsContactGroup使用此视图来显示DropDownList s。

我确定有一种简单的方法可以做到这一点,但我似乎无法看到解决方案。非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:0)

  1. 控制器返回的视图的视图模型,该视图具有#3的属性。
  2. 在Views \ Shared文件夹中的HTML Helper
  3. 在HTML Helper中定义属性的对象。
  4. 在控制器中填充定义HTML帮助程序的对象。