Hi
I am trying to put multiple fields in a SelectList that populates a DropDownList.
I can populate the list fine with the below code
Controller
var product = new ProductService().ProductByClubView(clubname.Trim()).ToList();
ViewBag.ProductList = new SelectList(product, "prodname", "prodname");
public IEnumerable<Product> ProductByClubView(string clubname)
{
var context = new dbS4CEntities(myprop.Connection);
var result = from a in context.Product
where a.clubname.Trim() == clubname.Trim()
orderby a.id descending
select a;
return result;
}
View
@model S4C.Models.PaymentView2Model
@using (Html.BeginForm("Payment2", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
class="control-label col-md-2">Product
<div class="col-md-10">
@Html.DropDownListFor(model => model.prodname, (SelectList)ViewBag.ProductList, new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })
</div>
</div>
</div>
}
This selects the products that belong to a club.
I want the drop down box to grab the products and the amount.
What I have tried:
So I thought the below code would work.
var product = new ProductService().ProductByClubView(clubname.Trim()).ToList();
var result = db.Product
.Where(x => x.clubname == clubname.Trim())
.Select(x => new
{
prodname= x.prodname,
Name = x.prodname + " - $" + x.amount.ToString()
});
ViewBag.ProductList = new SelectList(product, "prodname" , "Name" );
However, I am getting an error System.Web.HttpException: DataBinding: 'S4C.Models.Product' does not contain a property with the name 'Name'
How do I bind multiple fields to a dropdownlist?