我对MVC很新。我有以下Razor代码:
@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”是存储在ViewBag中的SelectedListItem
列表(我不想为所有下拉列表创建模型)。它是通过这种方法构建的:
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);
}
当用户将视图发回我的控制器并执行此方法时,我无法想象如何从下拉列表中获取所选制造商。
答案 0 :(得分:1)
使用
public ActionResult Create(BicycleSellerListing bicyclesellerlisting, FormCollection collection)
{
...
您可以获取所有输入,包括下拉所选项目,例如 collection [“ManufacturerList”] 或类似项目,具体取决于您的下拉列表名称。