MVC2 SelectedListItem 未绑定到 ViewModel
我的选择列表项有问题,其值是从数据库中检索的。
它在视图中显示项目列表,但不会将所选值传递(POST)到模型中。
因此,当用户提交或页面因验证而重新加载时,选择值 (PositionApplied) 为空。
谁能给我一些关于我哪里出错的指示?
在我的控制器中:
[HttpGet]
public ActionResult Index()
{
PopulateJobsDropdown();
return View();
}
private void PopulateJobsDropdown()
{
IEnumerable<SelectListItem> items = _service.GetJobs()
.Select(c => new SelectListItem
{
Value = c.JobID.ToString(),
Text = c.JobTitle
});
ViewData["PositionApplied"] = items;
}
在我的 ViewModel 中
public IEnumerable<SelectListItem> PositionApplied { get; set; }
在我的视图中
<%=Html.DropDownList("PositionApplied")%>
提前感谢您的指点!
I am having a problem with a selectlistitem, the values of which are being retrieved from a database.
It is displaying the list of items in the view, but it is not passing through (POSTing) the selected value into the model.
So when the user submits, or the page reloads due to validation, the select value (PositionApplied) is empty.
Can anyone give me some pointers as to where I am going wrong?
In my controller:
[HttpGet]
public ActionResult Index()
{
PopulateJobsDropdown();
return View();
}
private void PopulateJobsDropdown()
{
IEnumerable<SelectListItem> items = _service.GetJobs()
.Select(c => new SelectListItem
{
Value = c.JobID.ToString(),
Text = c.JobTitle
});
ViewData["PositionApplied"] = items;
}
In my ViewModel
public IEnumerable<SelectListItem> PositionApplied { get; set; }
In my View
<%=Html.DropDownList("PositionApplied")%>
Thanks in advance for any pointers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的代码行在哪里
那么,进入
类似以下内容
:您可以在视图中简单使用:
...
或者在 MVC2 下是否发生了一些我不知道的自动魔法?当我一直使用我给你的例子时。
添加
以避免 Linq to Entities 错误(如果您正在使用它)将您的方法更改为
,一切都会正常工作。
So, where is the code line that get's the
into
something like:
and you can simple use in your View:
...
or is there some of automagical happening under MVC2 that I'm not aware about? As I use the example I give you, all the time.
Added
in order to avoid Linq to Entities error (if you are using it) change your method to
and all will work fine.