1

在 ASP MVC 淹没列表中使用默认值的情况下,我需要代码返回所有值或简单地忽略该特定搜索条件。

下面是我在视图和控制器中的代码。由于 '%' 似乎不起作用,是否还有另一个关键字/运算符可以完成这项工作?

看法:

        <form method="post">
          <select name="Table" title="Table" style="font-size:8pt;">
            <option value="%">--Table Name--</option>
            <option value="AgentContEd">CE</option>
            <option value="AgentProductTraining">PT</option>
          </select>
          <select name="IssueType" style="font-size:8pt;">
            <option value="%">--Issue Type--</option>
            <option value="W">Warning</option>
            <option value="E">Error</option>
          </select>
          <select name="Status" style="font-size:8pt;">
            <option value="%">--Status Type--</option>
            <option value="O">Open</option>
            <option value="U">Under Review</option>
          </select>


        <input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" />
        <a href="#" style="padding-left: 30px;"></a>
        </form>

控制器:

    public ViewResult Index(FormCollection dropDownSelection)
    {
        //security
        //if (!Security.IsViewer(User)) return RedirectToAction("Message", "Home", new { id = 2 });

        //if (ViewBag.Level == 0) return RedirectToAction("Message", "Home", new { id = 2 });

        string table = dropDownSelection["Table"];
        string issue = dropDownSelection["IssueType"];
        string status = dropDownSelection["Status"];

        var followUpItem = from follow in db.FollowUpItems
                           where follow.TableName.Equals(table) &&
                                 follow.IssueType.Equals(issue) &&
                                 follow.Status.Equals(status)
                           select follow;

        return View(followUpItem.ToList());
    }
4

2 回答 2

2

您可以使用其中一种方法,也可以SqlMethods.Like只使用该String.Contains方法。(请记住,如果您保留使用或任何其他 SQL 通配符,String.Contains则会出现问题。)%

因此,这三种变体看起来像:

var followUpItem = from follow in db.FollowUpItems
                   where SqlMethods.Like(follow.TableName, table) &&
                         follow.IssueType.Contains(issue) &&
                         follow.Status.Equals(status)
                   select follow;
于 2013-01-22T21:24:09.833 回答
1

我还没有编译这个,但我猜你想要这样的东西:

 public ViewResult Index(FormCollection dropDownSelection)
    {
        //security
        //if (!Security.IsViewer(User)) return RedirectToAction("Message", "Home", new { id = 2 });

        //if (ViewBag.Level == 0) return RedirectToAction("Message", "Home", new { id = 2 });

        string table = dropDownSelection["Table"];
        string issue = dropDownSelection["IssueType"];
        string status = dropDownSelection["Status"];

        var followUpItem = from follow in db.FollowUpItems
                   where (follow.TableName.Equals(table) || table.Equals("%")) &&
                         (follow.IssueType.Equals(issue) || issue.Equals("%")) &&
                         (follow.Status.Equals(status) || status.Equals("%"))
                   select follow;

        return View(followUpItem.ToList());
    }
于 2013-01-22T21:24:24.380 回答