Reputation: 20733
I need on one view to only choose between several element in a list. Element of my list are complex type.
I receive in the viewBag the list of all elements I can choose.
I tried several things without success, the most approaching thing of what I want is this:
@{SelectList list = new SelectList(ViewBag.Entities, Model);}
@Html.DropDownListFor(x => x, list);
Have you an idea about how to use this?
EDIT: Tried this too:
@Html.DropDownListFor(x => x, new SelectList(ViewBag.Entities, "", "Name"));
EDIT2: also tried to change my model, having a "int" as id, representing the current id of entity
@Html.DropDownListFor(x => x, new SelectList(ViewBag.Entities, "Id", "Name"));
Still have this exception:
System.ArgumentException was unhandled by user code
Message=Value cannot be null or empty.
Parameter name: name
Source=System.Web.Mvc
ParamName=name
StackTrace:
at System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.SelectExtensions.DropDownListFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.SelectExtensions.DropDownListFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList)
at ASP._Page_Areas_Account_Views_Auth_EntityChooser_cshtml.Execute() in d:\Workspace\XYZ\Main\Code\AdManager\Areas\Account\Views\Auth\EntityChooser.cshtml:line 20
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
InnerException:
Upvotes: 1
Views: 4023
Reputation: 20733
Solution I finally found a way to do it:
@Html.DropDownListFor(x => x.SelectedId, new SelectList(
Model.AvailableEntities, "Id", "Name"));
I had to put a property on my model which can store the value. It seems that the DropDownListFor doesn't really likes that I've this x=>x
Upvotes: 2
Reputation: 19782
The method you're calling, defines Model as the SelectedValue
. You should give the names to use for dataValueField
and dataTextField
:
@{SelectList list = new SelectList(ViewBag.Entities, "Key", "Value");}
@Html.DropDownListFor(x => x, list);
Given that ViewBag.Entities
is a Dictionary
. If it contains a list of your own objects, simply point to the properties you want to use, instead of Key and Value.
Upvotes: 1