Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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?
Posted
Updated 18-Jan-18 11:33am
Comments
David_Wimbley 18-Jan-18 17:06pm    
Couple of issues, we don't know what your Product class looks like (S4c.Models.Product) so would need you to use improve question to add that.

Also, it looks like you are attempting to set ViewBag.ProductList = new SelectList(product) but you indicate its for "prodname", "Name". Do you need to swap out ViewBag.ProductList = new SelectList(product, "prodname" , "Name" ); to be ViewBag.ProductList = new SelectList(result, "prodname" , "Name" );
since the result collection is the only thing i see with a propery of prodname and Name.
elmbrook 18-Jan-18 17:26pm    
Duh....That was so dumb. Didn't realise I had called it result and still had product defined the line above. I think I need more sleep!
Thanks David

1 solution

var product = new ProductService().ProductByClubView(clubname.Trim()).ToList();


var result product = 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" );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900