使用复选框按MVC 5中的布尔列过滤表

时间:2018-11-28 19:39:19

标签: asp.net asp.net-mvc-4

我希望能够按兴趣过滤“联系人”表中的内容,并且我目前正尝试通过代表每个个人兴趣的一系列布尔属性来实现此目的。计划是我将在每个兴趣页的索引页面上具有一系列复选框,这将允许用户仅显示已将每个选中的复选框的一个或多个适当兴趣变量设置为“ true”的联系人(如果未选中任何复选框,则为所有联系人)。据我所知,这在理论上是可行的,但是到目前为止,我的研究还没有得出任何关于如何以这种特定方式执行这种过滤的结果。这里有人可以指出正确的方向吗?

编辑:这是我到目前为止与问题相关的代码:

EDIT2:我名义上有一组工作复选框,但是现在过滤不能完全按照我想要的方式工作。我希望控制器仅将联系人具有一个或多个相同变量设置为“ true”的复选框发送为相应的复选框,或者如果未选中任何复选框,则发送所有联系人的视图。我现在所拥有的对于单个选择都可以正常工作,但是对于多个选择,只能返回与 all 复选框匹配的条目。我想知道如何比现在更有效地进行条件过滤。

这是我的模型-“ Interests”字符串只是为了使其在Index中比在一行显示为灰色的复选框中更具表现力:

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>

这是我的Contacts控制器的Index ActionResult;再次,它是粗糙的,但名义上可以运行,只是不像我希望的那样:

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

0 个答案:

没有答案