How to fill dropdown list statically(Hardcoded)
I have 2 items
Text="Active" value="A"
Text="InActive" value="I"
and above values saved into database
Thanks,
How to fill dropdown list statically(Hardcoded)
I have 2 items
Text="Active" value="A"
Text="InActive" value="I"
and above values saved into database
Thanks,
If you want to do this all in a view, the code would look like this:
@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("Active", "A");
dictionary.Add("InActive", "I");
SelectList list = new SelectList(dictionary, "value", "key");
}
@Html.DropDownList("name", list)
Also if you wanted to add a blank, it would look like this:
@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("", "");
dictionary.Add("Active", "A");
dictionary.Add("InActive", "I");
SelectList list = new SelectList(dictionary, "value", "key");
}
@Html.DropDownList("name", list)
And if you wanted to have a particular item selected, it would look like this:
@{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("", "");
dictionary.Add("Active", "A");
dictionary.Add("InActive", "I");
SelectList list = new SelectList(dictionary, "value", "key", "I");
}
@Html.DropDownList("name", list)
Notice I changed the SelectList
line adding a "I"
parameter, this corresponds to the 'InActice' value causing it to be selected on page load.
This code would generate this in HTML:
<select id="name" name="name">
<option value="A">Active</option>
<option selected="selected" value="I">InActive</option>
</select>
So if this is going to be static you can just use this. You can remove the selected="selected"
if needed.
I'm not exactly sure what you mean by "above values saved into database", do you have a model to go with these items? If you edit your question and add some examples of what your trying to do I can help you better.