Dropdownlist 值未从传递到 ViewData 中视图的 SelectList 中设置
我正在尝试填充办公地点(文本)和地址(值)的下拉列表 显示页面后在浏览器中查看页面源时,我可以看到选择(下拉列表)选项值都是“”。 这是我的代码。 我正在使用 LinqToSql 数据上下文调用来获取 SelectList 的数据。 在调试器中,我可以看到从我的调用返回的列表实际上包含我需要的地址和办公地点。 我的模型数据上下文代码:
public partial class uls_dbDataContext
{
public List<office_location> GetOfficeLocations()
{
return office_locations.ToList();
}
}
我的控制器代码:
public ActionResult Directions()
{
uls_dbDataContext ulsdb_dc = new uls_dbDataContext();
ViewData["OfficeLocations"] = new SelectList(ulsdb_dc.GetOfficeLocations(),"location_address", "location_name");
ViewData["Title"] = "Directions";
return View();
}
我的视图代码: <%= Html.DropDownList("位置", (SelectList)ViewData["OfficeLocations"])%>
I am trying to populate a dropdownlist of office locations(text) and addresses (value)
When viewing my page source in my browser after displaying the page I can see that the select (dropdownlist) option values are all "". Here is my code. I am using a LinqToSql data context call to get my data for the SelectList. In the debugger I can see that the list returned from my call does in fact have the addresses and office locations I need.
My model datacontext code:
public partial class uls_dbDataContext
{
public List<office_location> GetOfficeLocations()
{
return office_locations.ToList();
}
}
My controller code:
public ActionResult Directions()
{
uls_dbDataContext ulsdb_dc = new uls_dbDataContext();
ViewData["OfficeLocations"] = new SelectList(ulsdb_dc.GetOfficeLocations(),"location_address", "location_name");
ViewData["Title"] = "Directions";
return View();
}
My view code:
<%= Html.DropDownList("locations", (SelectList)ViewData["OfficeLocations"])%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须从预览版 4 升级到 MVC RC2。它解决了下拉列表问题并解决了我的问题。
I had to upgrade to MVC RC2 from preview 4. It addressed drodownlist issues and fixed my problem.