我有以下MVC3模型:MVC3 DropDownList的代码首先
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”转换成“系统。 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; }
}
}
这是我的控制器:因为我是很新的MVC3谢谢
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;
}
}
}
任何帮助appriciated!
感谢您的回复,我该如何将它变成视图模型呢?对不起,我对MVC – user1302054 2012-04-22 20:02:51
@ user1302054很新 - 你不这样做。这就是要点 – 2012-04-22 21:41:30