I have an dropdownlist like so:
@Html.DropDownList("DeliveryOptions",
(IEnumerable<SelectListItem>)ViewData["DeliveryOptions"])
This gets its data from the controller action like so:
var options = context.DeliveryTypes.Where(x => x.EnquiryID == enqId);
ViewData["DeliveryOptions"] = new SelectList(options, "DeliveryTypeId",
"CODE" + " - " + "DeliveryPrice");
I’d like to have my dropdownlist display the CODE + DeliveryPrice in its text field, for example: ‘TNTAM – 17.54’ but I get the following error:
DataBinding: 'MyApp.Models.DeliveryTypes' does not contain a property
with the name 'CODE - DeliveryPrice'.
My DeliveryType Model looks like so:
[Key]
public int DeliveryTypeId { get; set; }
public string CODE { get; set; }
public decimal DeliveryPrice { get; set; }
You could use an anonymous type:
Or alternatively create a specific
CustomSelectListItemclass which you can reuse, which containsValueandTextproperties which you can reuse for such occasions as this.