加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – HTML.DropdownList – 文本字段显示多列的值

发布时间:2020-12-16 09:36:56 所属栏目:asp.Net 来源:网络整理
导读:我有一个这样的下拉列表: @Html.DropDownList("DeliveryOptions",(IEnumerableSelectListItem)ViewData["DeliveryOptions"]) 这从控制器动作获取其数据,如下所示: var options = context.DeliveryTypes.Where(x = x.EnquiryID == enqId);ViewData["Deliver
我有一个这样的下拉列表:

@Html.DropDownList("DeliveryOptions",(IEnumerable<SelectListItem>)ViewData["DeliveryOptions"])

这从控制器动作获取其数据,如下所示:

var options = context.DeliveryTypes.Where(x => x.EnquiryID == enqId);
ViewData["DeliveryOptions"] = new SelectList(options,"DeliveryTypeId","CODE" + " - " + "DeliveryPrice");

我想让我的下拉列表在其文本字段中显示CODE DeliveryPrice,例如:’TNTAM – 17.54’但是我收到以下错误:

DataBinding: 'MyApp.Models.DeliveryTypes' does not contain a property 
with the name 'CODE - DeliveryPrice'.

我的DeliveryType模型如下所示:

[Key]
public int DeliveryTypeId { get; set; }
public string CODE { get; set; }
public decimal DeliveryPrice { get; set; }

解决方法

您可以使用匿名类型:

var options = context.DeliveryTypes
    .Where(x => x.EnquiryID == enqId)
    .Select(x => new { Value = x.DeliveryTypeId,Text = x.CODE + " - " + x.DeliveryPrice });

ViewData["DeliveryOptions"] = new SelectList(options,"Value","Text");

或者创建一个可以重用的特定CustomSelectListItem类,其中包含Value和Text属性,您可以在这种情况下重复使用它们.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读