Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.dropdownlist with static content

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

  1. Exact match

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

like image 421
John John Avatar asked Feb 08 '26 03:02

John John


2 Answers

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" } });
}
like image 111
hutchonoid Avatar answered Feb 12 '26 04:02

hutchonoid


Why do you need the HTML-helper when only using static data?

<select id="myDropDownList" name="myDropDownList">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Or this perhaps:

@{
var list = new SelectList(new []
    {
        new {ID="1", Name="volvo"},
        new {ID="2", Name="saab"},
        new {ID="3", Name="mercedes"},
        new {ID="4", Name="audi"},
    },
    "ID", "Name", 1);
}
@Html.DropDownList("list", list)
like image 25
cederlof Avatar answered Feb 12 '26 04:02

cederlof