Mvc - 下拉列表中选择列表中的多个字段 [英] Mvc - multiple fields in selectlist for dropdownlistfor
问题描述
嗨
我试图将多个字段放在填充DropDownList的SelectList中。
我可以使用以下代码填写清单。
控制器
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">
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" );
但是,我收到一个错误System.Web.HttpException:DataBinding:'S4C.Models.Product'不包含名称为'的属性姓名'
如何将多个字段绑定到下拉列表?
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?
推荐答案
+ x.amount.ToString()
});
ViewBag.ProductList = new SelectList(product, prodname, Name);
" + x.amount.ToString() }); ViewBag.ProductList = new SelectList(product, "prodname" , "Name" );
但是,我收到一个错误System.Web.HttpException:DataBinding:'S4C.Models。产品'不包含名称为'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?
var product = new ProductService()。ProductByClubView(clubname.Trim())。ToList();
var结果product = db.Product
.Where(x => x.clubname == clubname.Trim())
。选择(x =>新
{
prodname = x.prodname,
Name = x.prodname + -
var product = new ProductService().ProductByClubView(clubname.Trim()).ToList();
varresultproduct = 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);
" + x.amount.ToString()
});
ViewBag.ProductList = new SelectList(product, "prodname" , "Name" );
这篇关于Mvc - 下拉列表中选择列表中的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!