【问题标题】:Loading model to HTML DropDownListFor helper将模型加载到 HTML DropDownListFor helper
【发布时间】:2016-11-04 18:42:28
【问题描述】:

我有一个名为 Region 的简单模型,就像

namespace App
{
    using System;
    using System.Collections.Generic;

    public partial class Region
    {
        public int RegionID { get; set; }
        public string RegionDescription { get; set; }
    }
}

我正在尝试将模型加载到 HTML DropDownListFor() 帮助程序中,例如

@model IEnumerable<App.Region>

<div class="row">
    @Html.DropDownListFor("RegionList",
                    new SelectList(model => model.RegionDescription),
                    "Select Region",
                    new { @class = "form-control" })

</div>

@model IEnumerable<App.Region>
<div class="row">
    @Html.DropDownListFor("RegionList",
                        new SelectList(s => s.RegionDescription),
                        "Select Region",
                        new { @class = "form-control" })

</div>

或:

@model IEnumerable<App.Region>
<div class="row">
@foreach (var item in Model)
{
    @Html.DropDownListFor("RegionList",
                        new SelectList(model => item.RegionDescription),
                        "Select Region",
                        new { @class = "form-control" })
}
</div>

但在这两种情况下我都会收到此错误。

无法将 lambda 表达式转换为类型“对象”,因为它不是 委托类型

为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • SelectList 构造函数确实需要一个集合。不是 lamda 表达式。你从哪里得到这些信息的?
  • 嗨,谢谢Shyju的评论,我关注了Link(tutorialsteacher.com/mvc/…)
  • 您提供的链接没有任何代码,例如您的问题!
  • 这部分怎么样@Html.DropDownListFor(m =&gt; m.StudentGender,

标签: asp.net-mvc html-helper


【解决方案1】:

SelectList 构造函数将集合作为第一个参数。不是lamda表情!您以不正确的方式使用辅助方法!

理想情况下,Html.DropDownListFor 方法的第一个参数是一个表达式,帮助程序可以从中获取视图模型属性的值。这意味着,要使用它,您的视图模型应该有一个属性来存储选定的选项值。所以创建一个这样的视图模型

public class CreateVm
{
  public int SelectedRegion { set;get;}
  public List<SelectListItem> Regions { set;get;}
}

现在在您的 GET 操作中,您需要创建此视图模型的对象,加载 Regions 集合属性并将其发送到视图

public ActionResult Create()
{
  var vm = new CreateVm();
  vm.Regions = new List<SelectListItem>{
         new SelectListItem { Value="1", Text="Region1"},
         new SelectListItem { Value="2", Text="Region2"},
         new SelectListItem { Value="3", Text="Region3"}
  };
  return View(vm);
}

在你的视图中,这个新视图模型是强类型的,你可以像这样使用DropDownListFor帮助方法

@model CreateVm
@Html.DropDownListFor(x=>x.SelectedRegion,Model.Regions,"Select Region",
                                                            new { @class = "form-control" })

或者如果您确实想使用传递给视图的现有视图模型/类型,您可以考虑使用 Html.DropDownList 辅助方法来呈现 SELECT 元素

@Html.DropDownList("SelectedRegion", 
                    new SelectList(Model, "RegionID", "RegionDescription"), "Select Region",
                    new { @class = "form-control" })

这将呈现一个名为 "SelectedRegion" 的 SELECT 元素

【讨论】:

  • 谢谢 Shyju,你代码的最后一部分正在做我正在寻找的事情,但我不明白你为什么要在 SelectList 中传递 3 个参数 Model, "RegionID", "RegionDescription"
  • 这里的模型是您的自定义类Region 的列表。您需要告诉 SelectList 对选项值使用“RegionId”属性值,对选项文本使用“RegionDescription”属性值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
相关资源
最近更新 更多