我有以下 MVC3 模型:
public class Dine
{
[Key]
[Display(Name = "Table Number")]
public string TableNumber { get; set; }
public List<SelectListItem> TableSelections { get; set; }
[Display(Name = "Table Date")]
[Required(ErrorMessage = "Please enter a date")]
public DateTime Date { get; set; }
}
}
列表项值在控制器中定义,下拉列表在视图中显示良好,仅当按下提交以将值保存到数据库时,我收到以下错误:
有谁知道如何解决这个问题?
它基于本教程http://codeoverload.wordpress.com/2011/05/22/dropdown-lists-in-mvc-3/
谢谢
编辑我遵循了下面的建议并收到此错误:无法将类型'System.Collections.Generic.List'转换为'System.Web.Mvc.SelectList'
这是我的看法:
<td>@Html.DropDownListFor(model => model.TableSelection, (SelectList)ViewBag.Tables)</td>
这就是我的模型中的内容
public class Dine
{
[Key]
[Display(Name = "Table No")]
public int TableNo { get; set; }
public string TableOption { get; set; }
[Display(Name = "Table Date")]
[Required(ErrorMessage = "Please enter a date")]
public DateTime Date { get; set; }
}
}
这是我的控制器:
public class RestaurantController : Controller
{
TheDatabaseEntities db = new TheDatabaseEntities();
//
// GET: /Dine/
public ActionResult Index()
{
ViewBag.Tables = GetTableOptions();
return View();
}
[HttpPost]
public ActionResult Index(Restaurant model)
{
if (ModelState.IsValid)
{
return RedirectToAction("AddBookingComplete");
}
ViewBag.Tables = GetTableOptions();
return View(model);
}
public ActionResult BookingComplete()
{
return View();
}
private List<Restaurant> GetTableOptions()
{
List<Restaurant> Tables = new List<Restaurant>();
Table.Add(new Dine() { TableNumber = 1, TableSelection = "Table 1" });
Table.Add(new Dine() { TableNumber = 2, TableOSelection = "Table" });
Table.Add(new Dine() { TableNumber = 3, TableSelection = "Table" });
Tables.Add(new Dine() { TableNumber = 4, TableSelection = "Table" });
return Table;
}
}
}
任何帮助都会得到帮助,因为我对 MVC3 很陌生,谢谢!