2
我正在编写需要与dropdownlist绑定的MVC列表页面。
由于我是ASP.net MVC的初中,我不知道如何使dropdownlist正确运行并动态选择。使用模型类绑定Asp.net MVC dropdownlist
我有两个模型类
public class CycleType
{
public int CycleTypeID { get; set; }
public string Type { get; set; }
public List<CycleModel> CycleModels { get; set; }
}
-----------------------------------------------------------
public class CycleModel
{
public int CycleModelID { get; set; }
public int CycleTypeID { get; set; }
public string Model { get; set; }
public virtual CycleType CycleType { get; set; }
}
然后一个Controller类,
public class CycleModelController : Controller
{
UnitOfWork<CycleModel> unitOfWork = new UnitOfWork<CycleModel>();
UnitOfWork<CycleType> unitOfWork_cycleType = new UnitOfWork<CycleType>();
...
[HttpGet]
public ActionResult Edit(int CycleModelID)
{
CycleModel cycleModel = unitOfWork.GenericTEntityRepository.GetByID(CycleModelID);
ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)));
return View(cycleModel);
}
...
}
然后一个刮刀文件,
<div class="editor-field">
@*Html.DropDownList("CycleType")*@
@*Html.EditorFor(model => model.CycleTypeID)*@
@Html.DropDownListFor(model => model.CycleTypeID,
new SelectList(ViewBag.CycleType, "Type", "CycleTypeID"))
@Html.ValidationMessageFor(model => model.CycleTypeID)
</div>
当我运行我的程序,我得到错误信息
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Type'.
1)我该如何纠正这段代码?
2)我怎样才能动态选择项目?
每一个建议将非常感激。
非常感谢你的回答非常有帮助@Eranga。 – 2012-01-10 09:43:14