Html.dropdownlist with static content

asp.net, asp.net-mvc, asp.net-mvc-4, c#, html-helper

Solution

Your first option is to include the html within your view:

<select id="selection">
    <option>Exact match</option>
    <option>Starts with</option>
</select>

Second option is to use a hard-coded built in html helper:

@Html.DropDownList("selection", new List<SelectListItem>() {new SelectListItem { Text="Exact match", Value = "Match"}, new SelectListItem { Text="Starts With", Value = "Starts"}})

The third option which I would prefer if it is used a lot on your site is to create an html helper extension and you can simply use it like this:

@Html.SearchSelectionList()

Here is the code for this:

public static MvcHtmlString SearchSelectionList(this HtmlHelper htmlHelper)
{
    return htmlHelper.DropDownList("selection", new List<SelectListItem>() { new SelectListItem { Text = "Exact match", Value = "Match" }, new SelectListItem { Text = "Starts With", Value = "Starts" } });
}

Problem

I am working on an asp.net mvc web application , and on my advance search page i want to have three `html.dropdownlist` which contain static values:- Exact match Start With and i need the dropdownlists to be beside any of the search field. so can any one advice how i can create such static `html.dropdownlist`, as all the current dropdownlists which i have are bing populated with dynamic data from my model ? Thanks

Original source

Related problems