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: