我用一個下拉列表中我create.aspx之一,但它的一些似乎沒有怎麼工作...我在做什麼asp.net-mvc dropdownlist錯誤?
public IEnumerable<SelectListItem> FindAllMeasurements()
{
var mesurements = from mt in db.MeasurementTypes
select new SelectListItem
{
Value = mt.Id.ToString(),
Text= mt.Name
};
return mesurements;
}
和我的控制器,
public ActionResult Create()
{
var mesurementTypes = consRepository.FindAllMeasurements().AsEnumerable();
ViewData["MeasurementType"] = new SelectList(mesurementTypes,"Id","Name");
return View();
}
和我create.aspx有這個,
<p>
<label for="MeasurementTypeId">MeasurementType:</label>
<%= Html.DropDownList("MeasurementType")%>
<%= Html.ValidationMessage("MeasurementTypeId", "*") %>
</p>
當我執行此我得到了這些錯誤,
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a
property with the name 'Id'.
@Ya darin that worked ...如何將「選擇」添加爲該列表中的第0個索引? – 2010-05-03 06:23:48
+1 Darin :) arg,我太慢了:( – 2010-05-03 06:25:32
@PieterG如何在該列表中添加「Select」作爲第0個索引? – 2010-05-03 06:26:19