MVC下拉列表中没有绑定到模型 [英] MVC Dropdown List isn't binding to the model
问题描述
我想建立一个简单的下拉列表中,但我不似乎能够得到它绑定到模型。
我使用Asp.Net MVC和NHibernate。
I am trying set up a simple dropdown list but I dont seem to be able to get it to bind to the Model. I am using Asp.Net MVC and nhibernate.
我的下拉列表中声明,像这样:
My dropdown list is declared like so:
<%= Html.DropDownListFor(model => model.Project, (IEnumerable<SelectListItem>)ViewData["Projects"], " -- Select -- ", new { name = "Project" })%>
我成立了选择列表,像这样:
I set up the select list like so:
ViewData["Projects"] = new SelectList(projectRepository.GetAll(), "EntityGUID", "Name", editEntity.Project);
这似乎绑定选择列表下拉罚款,但SelectedValue的未设置。
它显示为默认---选择---
This seems to bind the select list to the Dropdown fine, but the SelectedValue is not set. it shows up as the default --- Select ---
此外,当我保存这个数据,下拉不绑定到模型,我必须手动设置对象,像这样保存它:
Also when I save this data, the dropdown does not bind to the model, I have to manually set the object like so to save it:
entity.Project = projectRepository.GetById(new Guid(Request["Project"].ToString()));
我相信我有采取正确的messures直接有这个项目绑定到我的模型。
有什么事我是缺少在这里?
I believe I have take the correct messures to have this item bind directly to my model. Is there something I am missing here?
非常感谢您的时间,
杆
Many thanks for your time, Rod
推荐答案
OMG我发现这个问题........
OMG I found the problem........
我花了3天转:
<%= Html.DropDownListFor(model => model.Aspect, (IEnumerable<SelectListItem>)ViewData["AspectTypes"])%>
到
<%= Html.DropDownListFor(model => model.Aspect.EntityGUID, (IEnumerable<SelectListItem>)ViewData["AspectTypes"])%>
model.Aspect *的 .EntityGUID 的*
我不得不下拉绑定的GUID,而不是对象本身的对象。
卫生部....林感觉疼痛,许多工作赶上。
model.Aspect*.EntityGUID* I had to bind the drop down to the objects guid, not the object itself. Doh....Im feeling the pain, much work to catch up on.
感谢您的时间。
这篇关于MVC下拉列表中没有绑定到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!