1

In the case of a default value being used in an ASP MVC drown down list, I need the code to return all values or simply ignore that particular search criteria.

Below is the code I have in my View and Controller. Since the '%' does not seem to be working, is there another keyword/operator that will do the job?

View:

        <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>

Controller:

    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());
    }
NealR
  • 10,189
  • 61
  • 159
  • 299

2 Answers2

2

You can use either SqlMethods.Like or simply the String.Contains method. (Keep in mind that String.Contains will be problematic if you retain the use of % or any other SQL wildcards.)

So, the three variations would look like:

var followUpItem = from follow in db.FollowUpItems
                   where SqlMethods.Like(follow.TableName, table) &&
                         follow.IssueType.Contains(issue) &&
                         follow.Status.Equals(status)
                   select follow;
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

I haven't compiled this, but I'm guessing you want something like:

 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());
    }
JayC
  • 7,053
  • 2
  • 25
  • 41
  • This is basically what we're trying to do. However, we have the default value of the drop down list set to '%' so we don't have to worry about it in the controller if no value is selected by the user. – NealR Jan 22 '13 at 21:28