【问题标题】:MVC3 dropdown list that displays mutliple properties per itemMVC3 下拉列表,每个项目显示多个属性
【发布时间】:2011-12-03 19:51:49
【问题描述】:

我希望在我的视图中有一个下拉列表,其中显示患者的 ID、名字和姓氏。使用下面的代码,它会显示每个患者的名字。如何将所有三个属性都传递到 viewbag 并让它们显示在下拉列表中?

控制器

public ActionResult Create()
            {ViewBag.Patient_ID = new SelectList(db.Patients, "Patient_ID", "First_Name");
             return View();
            } 

查看

    <div class="editor-field">
                @Html.DropDownList("Patient_ID", String.Empty)
                @Html.ValidationMessageFor(model => model.Patient_ID)
            </div>

谢谢。

好的,我已按如下方式编辑了我的代码,我收到错误“没有具有键 'SelectedPatientId' 的 'IEnumerable' 类型的 ViewData 项。”

Controller


    public ActionResult Create()
            {
                var model = new MyViewModel();

        {
             var Patients = db.Patients.ToList().Select(p => new SelectListItem
            {
                Value = p.Patient_ID.ToString(),
                Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name)
            });

            var Prescribers = db.Prescribers.ToList().Select(p => new SelectListItem
    {
        Value = p.DEA_Number.ToString(),
        Text = string.Format("{0}-{1}-{2}", p.DEA_Number, p.First_Name, p.Last_Name)
    });

            var Drugs = db.Drugs.ToList().Select(p => new SelectListItem
            {
                Value = p.NDC.ToString(),
                Text = string.Format("{0}-{1}-{2}", p.NDC, p.Name, p.Price)
            });


        };
                return View(model);
            }

查看模型

    public class MyViewModel
        {
            [Required]
            public int? SelectedPatientId { get; set; }
            public IEnumerable<SelectListItem> Patients { get; set; }

            [Required]
            public int? SelectedPrescriber { get; set; }
            public IEnumerable<SelectListItem> Prescribers { get; set; }

            [Required]
            public int? SelectedDrug { get; set; }
            public IEnumerable<SelectListItem> Drugs { get; set; }
        }

查看

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

     @Html.DropDownListFor(
        x => x.SelectedPatientId,
        Model.Patients,
        "-- Select patient ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPatientId)
    <button type="submit">OK</button>

         @Html.DropDownListFor(
        x => x.SelectedPrescriber,
        Model.Patients,
        "-- Select prescriber ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPrescriber)
    <button type="submit">OK</button>
}

【问题讨论】:

  • 该错误通常意味着您的项目列表为空

标签: asp.net-mvc-3 properties drop-down-menu viewbag


【解决方案1】:

我建议您根本不要使用任何 ViewBag 并定义视图模型:

public class MyViewModel
{
    [Required]
    public int? SelectedPatientId { get; set; }
    public IEnumerable<SelectListItem> Patients { get; set; }
}

然后让你的控制器动作填充并将这个视图模型传递给视图:

public ActionResult Create()
{
    var model = new MyViewModel
    {
        Patients = db.Patients.ToList().Select(p => new SelectListItem
        {
            Value = p.Patient_ID.ToString(),
            Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name)
        });
    };
    return View(model);
} 

最后在强类型视图中显示下拉列表:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(
        x => x.SelectedPatientId, 
        Model.Patients, 
        "-- Select patient ---"
    )
    @Html.ValidationMessageFor(x => x.SelectedPatientId)
    <button type="submit">OK</button>
}

【讨论】:

  • 我不相信我可以使用这种方法。除了包含患者姓名列表的 DropDownList 之外,我还有其他包含来自其他实体的数据的下拉列表。我的 Create View 是 Models.Rx 的强类型。一个 Rx 包含患者、处方者和药物之间的关系。如果我按照您的方法,我将无法提取“MyViewModel”之外的任何内容,对吗?
  • @DanielHendrix,在更新的示例中,您已经正确添加了另一个字段来保存处方下拉菜单。然后在您的创建视图中实例化此视图模型 (var model = new MyViewModel()),然后查询您的数据库或其他任何内容,并将结果存储在一些局部变量中,例如患者、处方者和药物,但从不设置视图的任何属性模型。您需要在返回视图之前设置它们model.Patients = patients; ...
【解决方案2】:

实现这一点的简单方法是通过修改模型类或添加/修改部分类来在模型上创建附加属性

[NotMapped]
public string DisplayFormat
{
    get
    {
      return string.Format("{0}-{1}-{2}", Patient_ID, First_Name, Last_Name);
    }  
}

【讨论】:

  • 我在修改模型时收到以下错误。我使用了数据库优先方法。错误 1 ​​错误 3004:从第 561 行开始映射片段时出现问题:没有为 Set Patient.DisplayList 中的属性指定映射。在以下情况下,具有密钥 (PK) 的实体将不会往返:实体类型为 [CAHODModel.Patient]
  • 对,您应该在该参数前面加上 [NotMapped] 我已经更新了我的示例。请看msdn.microsoft.com/en-us/data/gg193958
猜你喜欢
  • 1970-01-01
  • 2012-08-16
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多