我想创建两个下拉列表,但数据出现在一个下拉列表中,如下图所示:
而另一个是空的
我想要B1和B2在一个下拉列表中
并在其他下拉列表中命名 zahra
控制器中的此代码:
// GET: Contracts/Create
public ActionResult Create()
{
var Sections = _context.Sections.ToList();
var Customers = _context.Customers.ToList();
List<SelectListItem> list = new List<SelectListItem>();
foreach (var item in Customers)
{
list.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });
ViewBag.Customers = list;
}
List<SelectListItem> list1 = new List<SelectListItem>();
foreach (var item in Sections)
{
list.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });
ViewBag.Sections = list1;
}
return View();
}
这是在视图中
<div class="form-group">
@Html.LabelFor(model => model.CustomerId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CustomerId, (IEnumerable<SelectListItem>)ViewBag.Customers, "Select customers", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CustomerId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SectionsId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SectionsId, (IEnumerable<SelectListItem>)ViewBag.Sections, "Select Sections", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SectionsId, "", new { @class = "text-danger" })
</div>
</div>
函数式编程
江户川乱折腾
撒科打诨
相关分类