如何处理许多可能的值进行查询? [英] How to deal with many possible values to make a query?

查看:118
本文介绍了如何处理许多可能的值进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个MVC应用程序中,用户将不得不使用大量的过滤器,以得到他想要什么的可能性。

I'm building an MVC app in which the user will have the possibilities of using a lot of filters to get exactly what he wants.

下面是一个基于控制器的方法这些过滤器的概述:

Here's an overview of those filters based on the controller method:

//
// GET: /Card/SearchIndex
public ActionResult SearchIndex(string objName, string objType, string objCostSymbol, string objCost, string powerSymbol,
                                        string powerValue, string ratingSymbol, string ratingValue, string ownerName, string objSet, 
                                        string objRarity, string addCostValue, int? objNumber,
                                        string addCostValue2, string addCostValue3, string addCostValue4, string addCostValue5, string addCostValue6,
                                        bool? blueColor, bool? redColor, bool? yellowColor, bool? purpleColor, bool? greyColor, bool? blackColor,
                                        bool? musicColor, bool? allColor)
        {
             // MORE CODE HERE...
        }

我想知道如何将处理所有这些过滤器的最佳方式,我怎么能拿objInfo的基础上,给出的参数列表。请记住,某些值可能为null。所有我所做的到现在是负载全能的objInfo我可以,然后通过消除非通缉的项目,这是我的感觉不聪明的排序,但我是新来的MVC应用程序与我米试图找到更好的方法来做到这一点。

I want to know how would be the best way to deal with all these filters, and how could I get a List of objInfo based on the given parameters. Keep in mind that some values may be null. All I've done up to now is load "all" the objInfo I could, then sort them by removing the non-wanted item, which is in my sense "not smart", but I'm new to MVC App and I'm trying to find a better way to do this.

修改

下面是生成的数据视图:

Here's the view that generates the data:

@using System.Web.Mvc.Html
@model PagedList.IPagedList<MvcApp.Models.ObjInfo>

@{
    ViewBag.Title = "SearchIndex";
}

<h2>Objects Management</h2>

<p>
    @Html.ActionLink("Create New Obj", "Create")
    @using (Html.BeginForm()){
        <p>
            <label>
                Obj Colors : Check a box to search for a color.
            </label>
            All: @Html.CheckBox("allColor", true)<br/>
            Blue: @Html.CheckBox("blueColor", true)
            Red: @Html.CheckBox("redColor", true)
            Yellow: @Html.CheckBox("yellowColor", true) <br/>
            Purple: @Html.CheckBox("purpleColor", true)
            Grey: @Html.CheckBox("greyColor", true)
            Black: @Html.CheckBox("blackColor", true)
            Music: @Html.CheckBox("musicColor", true)
        </p>
        <p>
            <label>
                Obj Values: Select a value in the list below.
            </label>
            Obj Number: <input type="number" min="0" max="9999" name="cardNumber" value="int" style="width: 70px"/><br/>
            Additional Cost (contains): @Html.DropDownList("addCost", String.Empty) + @Html.DropDownList("addCost2", String.Empty)
            + @Html.DropDownList("addCost3", String.Empty) + @Html.DropDownList("addCost4", String.Empty)
            + @Html.DropDownList("addCost5", String.Empty) + @Html.DropDownList("addCost6", String.Empty)  <br/>
            Cost: @Html.DropDownList("objCostSymbol", "=") @Html.DropDownList("objCost", String.Empty)<br />
            Power: @Html.DropDownList("powerSymbol", "=") @Html.DropDownList("powerValue", String.Empty)<br/>
            Rating: @Html.DropDownList("ratingSymbol", "=") @Html.DropDownList("ratingValue", String.Empty)<br />
            <label>
                Obj Text: Write a name, part of a name, or a word.
            </label>
            Obj Name: @Html.TextBox("objName") <br/>
            Owner: @Html.TextBox("ownerName") <br />
            <label>
                Obj Categories: Select a category in the list below.
            </label>
            Type: @Html.DropDownList("objType","All") <br/>
            Obj Set: @Html.DropDownList("objSet", "All") <br/>
            Rarity: @Html.DropDownList("objRarity", "All")<br />
            <div class="float-right">
                <input type="submit" value="Filter" name="submitbutton">
            </div>
        </p>
    }
</p>
<span style="color:red; font-size: 1.7em; font-style: italic;">@ViewData["ErrorMessage"]</span>
<table>
    <tr>
        <th>Obj Name</th>
        <th>Obj Number</th>
        <th>Obj Color</th>
        <th>Additional Cost</th>
        <th>Cost</th>
        <th>Obj Type</th>
        <th>@Html.ActionLink("Power", "SearchIndex", new {sortOrder=ViewBag.PowerSortParm})</th>
        <th>@Html.ActionLink("Rating", "SearchIndex", new {sortOrder=ViewBag.RatingSortParm})</th>
        <th>Rarity</th>
        <th>Obj Set Name</th>
        <th>Owner Name</th>
        <th></th>
    </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjColor)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjAddCost)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjCost)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjPower)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjRating)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjRarity)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjSet.m_ObjSetName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.m_ObjOwner)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.m_ObjID }) |
                @Html.ActionLink("Details", "Details", new { id=item.m_ObjID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.m_ObjID })
            </td>
        </tr>
    }
</table>

<div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount

    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "SearchIndex", new {page = 1, sortOrder = ViewBag.CurrentSort})
        @Html.Raw(" ")
        @Html.ActionLink("< Prev", "SearchIndex", new {page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort})
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "SearchIndex", new {page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort})
        @Html.Raw(" ")
        @Html.ActionLink(">>", "SearchIndex", new {page = Model.PageCount, sortOrder = ViewBag.CurrentSort})
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
</div>

任何意见将帮助我做一个更好的工作,谢谢你。

Any advice will help me do a better job, thank you.

推荐答案

你为什么不使用 ModelBinder的。我认为这是与你的情况非常有用。请参阅以下内容:

Why do you not use ModelBinder . I think it's very useful with your case. Please see below:

_我有一个模式

public class Employee
{
   public string Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime BirthDate { get; set; }
   public Address HomeAddress { get; set; }
}

ModelBinder的员工

public class EmployeeBinder : IModelBinder {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
            var emp = new Employee {
                                       Id = controllerContext.HttpContext.Request.Form["Id"],
                                       FirstName = controllerContext.HttpContext.Request.Form["FirstName"],
                                       LastName = controllerContext.HttpContext.Request.Form["LastName"],
                                       BirthDate = new DateTime(int.Parse(controllerContext.HttpContext.Request.Form["year"]),
                                           int.Parse(controllerContext.HttpContext.Request.Form["month"]),
                                           int.Parse(controllerContext.HttpContext.Request.Form["day"]))
                                   };

            return emp;
        }
    }

然后控制器

public ActionResult Example(Employee emp) {
            return View(emp);
}

网址:的http://本地主机:1034 /主页/实例ID = 1&安培;姓氏= lazycatit

这篇关于如何处理许多可能的值进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆