Alright, so here’s the deal. I would like to be able to create a custom dropdown box from an System.Linq.Expressions.Expression in c#. I know there are helpers, like DropDownListFor, but I’m not sure how to make one from scratch, or how to pull the values etc from the expression.
Essentially why I’m doing this is if you use DropDownListFor it will ‘select’ an <option> based on it’s value, but I want to ‘select’ an option based on it’s text instead. So if anyone has an idea how to do that instead, that would be great.
This is essentially what I have so far
public static MvcHtmlString ValidatedEditableDropDownListFor<TModel,TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
SelectList list,
String defaultOption,
Object attribs)
{
//Basically puts the strings into a html template<div>{0}{1}</div> etc.
return GetValidatedHtml(
htmlHelper.LabelFor(expression).ToString() ,
htmlHelper.DropDownListFor(expression , list , defaultOption , attribs).ToString()
,htmlHelper.GetValidatedErrorMessage(expression),false,"EditableDropDown");
}
Any ideas on how I can have the DropDownListFor make an <option> selected, or how to create my own html element based on the Expression and SelectList would be greatly appreciated.
Thanks.
Why not pass your list of SelectedListItems to your view with the correct one selected by it’s text from your controller