NealR
NealR

Reputation: 10669

Getting selected item from ASP MVC3 drop down list

I am trying, without any luck, to use the selected item from a drop down list in the controller. I've stepped through the code and can see that each of the string values are "null" after they should be assigned.

Below is the code from the View

<span id="searchBox" class="boxMe" >
      <select id="Table" title="Table" style="font-size:8pt;">
        <option value="default">--Table Name--</option>
        <option value="CE">CE</option>
        <option value="PT">PT</option>
      </select>
      <select id="IssueType" style="font-size:8pt;">
        <option value="default">--Issue Type--</option>
        <option value="W">Warning</option>
        <option value="E">Error</option>
      </select>
      <select id="Status" style="font-size:8pt;">
        <option value="default">--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>
</span>

And here is what I have in the controller. As this is a search filter I have this in the Index() method at the moment.

    public ViewResult Index(FormCollection dropDownSelection)
    {
        string table = dropDownSelection["Table"];
        string issue = dropDownSelection["IssueType"];
        string status = dropDownSelection["Status"];

        return View(db.FollowUpItems.ToList());
    }

Upvotes: 0

Views: 446

Answers (1)

Ant P
Ant P

Reputation: 25221

You should use the name attribute on your <select> tags, rather than id. The name is what is used as the key in the form post:

<select name="Table" title="Table" style="font-size:8pt;">

If you haven't already, you should also wrap your inputs in <form> tags:

<span ...>
    <form method="post">
        <!-- Inputs -->
    </form>
</span>

Upvotes: 2

Related Questions