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
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" } });
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With