【问题标题】:Asp.Net MVC DropDownListFor : how can the value be the whole objectAsp.Net MVC DropDownListFor:值怎么可能是整个对象
【发布时间】:2012-01-26 11:34:54
【问题描述】:

我需要在一个视图中仅在列表中的多个元素之间进行选择。我的列表元素是复杂类型。

我在 viewBag 中收到我可以选择的所有元素的列表。

我尝试了几件事都没有成功,我想要的最接近的事情是这样的:

@{SelectList list = new SelectList(ViewBag.Entities, Model);}
@Html.DropDownListFor(x => x, list);

你知道如何使用它吗?

编辑: 也试过这个:

@Html.DropDownListFor(x => x, new SelectList(ViewBag.Entities, "", "名称"));

EDIT2:也试图改变我的模型,有一个“int”作为id,代表实体的当前id

@Html.DropDownListFor(x => x, new SelectList(ViewBag.Entities, "Id", "名称"));

还是有这个异常:

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: 

【问题讨论】:

  • 你的模型是什么类型的?
  • 取决于我们正在谈论的尝试,我尝试使用我的自定义模型,int,...

标签: html asp.net-mvc drop-down-menu


【解决方案1】:

解决方案 我终于找到了一种方法:

@Html.DropDownListFor(x => x.SelectedId, new SelectList(
    Model.AvailableEntities, "Id", "Name"));

我必须在我的模型上放置一个可以存储值的属性。看来 DropDownListFor 不太喜欢我有这个 x=>x

【讨论】:

  • 哦。如果你告诉了你的模型在视图上的样子,我可以告诉你:)
  • 但在不同的测试中,我的模型不一样,在第一次测试中,我直接拥有一个“实体”类型的模型,之后我有了一个视图模型,我的对象作为属性,现在我有一个带有 id 的视图模型
  • 这是非常重要的信息,因为您可以看到您的解决方案要求您了解模型上的属性:)
【解决方案2】:

您调用的方法将 Model 定义为 SelectedValue。您应该给出用于dataValueFielddataTextField 的名称:

@{SelectList list = new SelectList(ViewBag.Entities, "Key", "Value");}
@Html.DropDownListFor(x => x, list);

鉴于ViewBag.EntitiesDictionary。如果它包含您自己的对象列表,只需指向您要使用的属性,而不是 Key 和 Value。

【讨论】:

  • 问题是我没有“要在“x”上设置的键值,我需要存储列表的整个对象
  • 您能发布 ViewBag.Entities 中的内容吗?复杂类型的代码?似乎您尝试做的事情是不可能的。您只能将键/值对列表放在下拉列表中。或者,您可以将对象序列化为 JSON 并将其放入 value 字段中。但恕我直言,这是一个非常糟糕的主意!
猜你喜欢
  • 2019-01-09
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2019-12-25
  • 2021-07-01
  • 2012-03-30
相关资源
最近更新 更多