asp.net - 在 MVC 5 中使用复选框按布尔列过滤表
问题描述
我希望能够按兴趣过滤我的“联系人”表的内容,我目前正试图通过代表每个个人兴趣的一系列布尔属性来实现这一点。计划是我将在索引页面上为每个兴趣设置一系列复选框,允许用户仅显示联系人,对于每个选中的复选框,将一个或多个适当的兴趣变量设置为“true” (如果未选中复选框,则为所有联系人)。据我所知,这在理论上是可行的,但到目前为止,我的研究还没有发现任何关于如何以这种特定方式执行这种过滤的结果。这里有人可以指出我正确的方向吗?
编辑:这是我迄今为止与该问题相关的代码:
EDIT2:我有一组名义上有效的复选框,但现在过滤并没有按照我想要的方式工作。我希望控制器仅发送视图,无论哪个联系人将一个或多个相同的变量设置为“true”作为相应的复选框,或者如果没有选中任何框,则发送所有联系人。我现在所拥有的对于单选来说效果很好,但是多选只会返回与所有复选框匹配的条目。我想知道如何比我现在更有效地进行有条件的过滤。
这是我的模型-“兴趣”字符串只是为了使它在索引中更形象,而不仅仅是一行灰色的复选框:
public class Contact : IValidatableObject
{
public int Id { get; set; }
public string Email { get; set; }
public string PhoneNum { get; set; }
public string Name { get; set; }
public bool LikesClassic { get; set; }
public bool LikesCountry { get; set; }
public bool LikesHipHop { get; set; }
public bool LikesMetal { get; set; }
public bool LikesPop { get; set; }
public bool LikesRap { get; set; }
public bool LikesRock { get; set; }
public string Interests
{
get
{
var interests = "";
if (this.LikesClassic)
interests = interests + "[Classic] ";
if (this.LikesCountry)
interests = interests + "[Country] ";
if (this.LikesHipHop)
interests = interests + "[Hip-Hop] ";
if (this.LikesMetal)
interests = interests + "[Metal] ";
if (this.LikesPop)
interests = interests + "[Pop] ";
if (this.LikesRap)
interests = interests + "[Rap] ";
if (this.LikesClassic)
interests = interests + "[Rock] ";
return interests;
}
}
这是我的视图-复选框已到位但尚未起作用
@model IEnumerable<ContactInterests.Models.Contact>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Classic: @Html.CheckBox("Classic")
Country: @Html.CheckBox("Country")
Hip-Hop: @Html.CheckBox("HipHop")
Metal: @Html.CheckBox("Metal")
Pop: @Html.CheckBox("Pop")
Rap: @Html.CheckBox("Rap")
Rock: @Html.CheckBox("Rock")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.PhoneNum)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
Interests
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNum)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Interests)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
这是我的联系人控制器的索引操作结果;再一次,它很粗糙,但名义上是功能性的,只是不是我喜欢的方式:
public ActionResult Index(string Classic, string Country, string HipHop,
string Metal, string Pop, string Rap, string Rock)
{
var contactList = from c in db.Contacts select c;
if (Classic == "true")
{
contactList = contactList.Where(c => c.LikesClassic == true);
}
if (Country == "true")
{
contactList = contactList.Where(c => c.LikesCountry == true);
}
if (HipHop == "true")
{
contactList = contactList.Where(c => c.LikesHipHop == true);
}
if (Metal == "true")
{
contactList = contactList.Where(c => c.LikesMetal == true);
}
if (Pop == "true")
{
contactList = contactList.Where(c => c.LikesPop == true);
}
if (Rap == "true")
{
contactList = contactList.Where(c => c.LikesRap == true);
}
if (Rock == "true")
{
contactList = contactList.Where(c => c.LikesRock == true);
}
if (Classic == "false" && Country == "false" && HipHop == "false" && Metal == "false" && Pop == "false" && Rap == "false" && Rock == "false")
{
contactList = from c in db.Contacts select c;
}
return View(contactList.ToList());
}
解决方案
推荐阅读
- android - PendingIntent.getActivity() 处的 NullPointerException
- algorithm - RBT 总是满的吗?
- asp.net - [Bind()] 中未列出的属性设置为 null
- sql - 更新具有串行主键的表和具有引用外键(postgres)的另一个表
- javascript - 如何在 wordpress 中从 ajax 调用 php 函数?
- pytorch - 使用 Pytorch 运行 Bert 时 CUDA 内存不足(以前工作过)
- javascript - 如何将所有键/值对从 forEach 推送到对象
- php - 如何拥有多个依赖用户输入的产品 Laravel
- android - Android:尽管有权限,但无法列出 /storage/emulated/0 文件
- python - 当我装饰 python 函数时,VS Code 在悬停时显示包装器的帮助,而不是函数的