我对 MVC 很陌生。我有以下剃刀代码:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset style="margin:5px">
<legend>List a Bicycle for Sale</legend>
<div class="editor-label">
@Html.LabelFor(model => model.BicycleManfacturer)
</div>
<div class="editor-field">
@Html.DropDownList("ManufacturerList")
</div>
....
....
<div class="float-right">
<input type="submit" value="List Bike" />
</div>
</fieldset>
}
“ManufacturerList”是SelectedListItem
存储在 ViewBag 中的列表(我不想为所有下拉列表创建模型)。它是通过这种方法构建的:
private void HydrateManufacturerList()
{
var manufacturerList = (from row in db.BicycleManufacturer.ToList()
select new SelectListItem
{
Text = row.Description,
Value = row.BicycleManufacturerId.ToString()
}).ToList();
manufacturerList.Add(new SelectListItem
{
Text = "-- Select Manufacturer --",
Value = "0",
Selected = true
});
ViewBag.ManufacturerList = manufacturerList.OrderBy(row => row.Text);
}
我有以下代码在提交完成时被调用:
[HttpPost]
public ActionResult Create(BicycleSellerListing bicyclesellerlisting)
{
bicyclesellerlisting.ListingDate = System.DateTimeOffset.Now;
if (ModelState.IsValid)
{
db.BicycleSellerListing.Add(bicyclesellerlisting);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(bicyclesellerlisting);
}
当用户将视图发布回我的控制器并执行此方法时,我无法弄清楚如何从下拉列表中获取所选制造商。