我在此表单上有三个表格。下拉列表是在我们的数据库中显示所有可能的品牌名称,并返回该品牌的UID。我尝试了许多不同的方法来解决这个问题,但无济于事。目前我收到错误消息,'无法将lambda表达式转换为类型字符串,因为它不是委托类型。'所以我必须使用DropDownList助手错误。
这是我的ViewModel:
public class AlternateCodeViewModel
{
//public web2_item_alternate_code web2_item_alternate_code { get; set; }
[Key]
[Column(Order = 0)]
public int inv_mast_uid { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
[Display(Name = "Alternate Code")]
public string Alternate_Code_Item_ID { get; set; }
[Key]
[Column(Order = 2)]
public int Alternate_Code_Brand_ID { get; set; }
[Required]
[StringLength(1)]
[Display(Name = "Exact Flag")]
public string Exact_Flag { get; set; }
[Required]
[StringLength(1)]
[Display(Name = "Delete Flag")]
public string Delete_Flag { get; set; }
public int item_brand_uid { get; set; }
public IEnumerable<web2_item_brands> web2_item_brands { get; set; }
public inv_mast inv_mast { get; set; }
}`
这是我的控制器:
这是我的观点:
<div class="form-group">
@Html.LabelFor(model => model.web2_item_brands, "Brand Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.web2_item_brands, Model.web2_item_brands, htmlAttributes: new { @class = "form-control" })
@*@Html.ValidationMessageFor(model => model.web2_item_brands, "", new { @class = "text-danger" })*@
</div>
</div>
答案 0 :(得分:1)
您必须将dropdownlist绑定到selectlistitems而不是其他复杂类型。这样的事情。
<强>模型强>
public class AlternateCodeViewModel
{
[Key]
[Column(Order = 0)]
public int inv_mast_uid { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
[Display(Name = "Alternate Code")]
public string Alternate_Code_Item_ID { get; set; }
[Key]
[Column(Order = 2)]
public int Alternate_Code_Brand_ID { get; set; }
[Required]
[StringLength(1)]
[Display(Name = "Exact Flag")]
public string Exact_Flag { get; set; }
[Required]
[StringLength(1)]
[Display(Name = "Delete Flag")]
public string Delete_Flag { get; set; }
public int item_brand_uid { get; set; }
public IEnumerable<web2_item_brands> web2_item_brands { get; set; }
public inv_mast inv_mast { get; set; }
}
public class inv_mast
{
}
public class web2_item_brands
{
public int item_brand_uid { get; set; }
public string item_brand_text { get; set; }
}
控制器操作
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new AlternateCodeViewModel
{
web2_item_brands =
new[]
{new web2_item_brands
{ item_brand_text = "text", item_brand_uid = 1}
}
});
}
}
标记代码
@using System.Web.Mvc.Html
@model Test.ViewModels.AlternateCodeViewModel
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(model => model.item_brand_uid,
Model.web2_item_brands.Select(s =>
new SelectListItem { Value = s.item_brand_uid.ToString(), Text = s.item_brand_text}),
htmlAttributes: new { @class = "form-control" })
</div>
</div>
注意:我不知道这个复杂类型web2_item_brands代表什么,因此我声明了一个上面同名的简单类,只是为了向您展示用法。