I am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns
based on passing in a model ( any ) what the property is for the value and same for text.
so i have something like this:
public static MvcHtmlString DataFilledDropDown<T>(this HtmlHelper h, string name, T selectedValue)
{
var myModel = typeof(T);
var dropDownBox = new Tag("select").With("name", name).And("id", name);
foreach (T val in Enum.GetValues(myModel))
{
var itemText = Resources.ResourceManager.GetString(val.ToString());
if (String.IsNullOrEmpty(itemText)) itemText = val.ToString();
var dft = new Tag("option")
.With("value", "0")
.AndIf(val.Equals(selectedValue), "selected", "selected")
.WithText("-- CHOOSE --");
dropDownBox.Append(dft);
var option = new Tag("option")
.With("value", (val).ToString())
.AndIf(val.Equals(selectedValue), "selected", "selected")
.WithText(itemText);
dropDownBox.Append(option);
}
return MvcHtmlString.Create(dropDownBox.ToString());
}
but this would only cover me for single fields but my model could be like this:
public class TestModel{
public int Id {get; set; } // this i want as a value in dropdown
public string Name {get;set;} // this i want to be the text value in dropdown
public bool isActive { get; set; } // this i dont need in dropdown but have data in model
}
so with above i want to then create something like this:
Html.DataFilledDropDown<myModel>("TestDropdown","Id","Name","0")
where the input is name of dropdown, name of value property, name of text property and default selected value
I have completed this with a little help from the answer below, this is the code for anyone interested:
public static MvcHtmlString DataFilledDropDown<T>(
this HtmlHelper h, string name, IEnumerable<T> items,
Func<T,string> valueField, Func<T,string> textField, string selectedValue)
{
var dropDownBox = new Tag("select").With("name", name).And("id", name);
var defaultValue = "0";
var dft = new Tag("option")
.With("value", "0")
.AndIf(defaultValue.Equals(selectedValue), "selected", "selected")
.WithText("-- CHOOSE --");
dropDownBox.Append(dft);
foreach (var item in items)
{
var myValue = valueField(item);
var myName = textField(item);
var option = new Tag("option")
.With("value", myValue)
.AndIf(myValue.Equals(selectedValue), "selected", "selected")
.WithText(myName);
dropDownBox.Append(option);
}
return MvcHtmlString.Create(dropDownBox.ToString());
}
and to run the code
<%: Html.DataFilledDropDown("SpexOptionType", Model.Clubs, x => x.clubID.ToString(), x => x.clubName, "0")%>
thats it
many thanks
I know this doesn’t answer your question directly, but I would suggest you move away from using strings for your name and ids. They are problematic and hard to maintain over time. Instead use Func.
Here’s what I use for select lists:
This is how it’s used: