MVC下拉列表不绑定到模型 [英] MVC Dropdown List isn't binding to the model

查看:167
本文介绍了MVC下拉列表不绑定到模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个简单的下拉列表,但我似乎无法让它绑定到模型。
我正在使用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()));

我相信我已经采取正确的方式让这个项目直接绑定到我的模型。
有没有我在这里遗失的东西?

I believe I have take the correct messures to have this item bind directly to my model. Is there something I am missing here?

非常感谢你的时间,
Rod

Many thanks for your time, Rod

推荐答案

OMG我发现问题........

OMG I found the problem........

我花了3天时间:

<%= Html.DropDownListFor(model => model.Aspect, (IEnumerable<SelectListItem>)ViewData["AspectTypes"])%>

into:

<%= Html.DropDownListFor(model => model.Aspect.EntityGUID, (IEnumerable<SelectListItem>)ViewData["AspectTypes"])%>

model.Aspect **。EntityGUID **
我不得不将下拉列表绑定到对象guid,而不是对象本身。
Doh ....我感到痛苦,很多工作要赶上。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆