Can't get the selected value of the viewdata dropdownlist to my controller action?

Viewed 55

I am having trouble getting the selected value of the DropDownList to my controller. The string I search is passed to the controller, but the selected value of the dropdown list is not.

Controller

public ActionResult Index()
{
    List<SelectListItem> items = new List<SelectListItem>();
    items.Add(new SelectListItem { Text = "Movies", Value = "0", Selected = true });
    items.Add(new SelectListItem { Text = "Tv Seires", Value = "1" });
    items.Add(new SelectListItem { Text = "Cast", Value = "2" });
    items.Add(new SelectListItem { Text = "All", Value = "3" });
    ViewData["Options"] = items;
    return View();
}

View

<div class="input-group">
    <div class="input-group-btn search-panel">
        @Html.DropDownList("Options",ViewData["Options"]as SelectList, new { @class = "btn btn-secondary dropdown-toggle", @id = "search_dropdown" })
    </div>
    @Html.TextBoxFor(m => m.searchValue,new{htmlAttributes = new {@class = "form-control", @id="search_input",placeholder="Search term..."}})
    <span class="input-group-btn">
        <input type="submit" value="&#x1f50d;" class="btn btn-secondary" id="search_button" />
    </span>
</div>
1 Answers

Consider putting it inside a form:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.DropDownList("Options",ViewData["Options"]as SelectList, new { @class = "btn btn-secondary dropdown-toggle", @id = "search_dropdown" })

    @Html.TextBoxFor(m => m.searchValue,new{htmlAttributes = new {@class = "form-control", @id="search_input",placeholder="Search term..."}})

    <input type="submit" value="&#x1f50d;" class="btn btn-secondary" id="search_button" />
}
Related