[英]ASP.NET Binding linq query results to HTML.DropDownList() using MVC
I am trying to build a drop down list with a single database call. 我正在尝试使用单个数据库调用构建一个下拉列表。
My table consists of an Id column and a CompanyName column. 我的表由Id列和CompanyName列组成。 I need to display the CompanyName to the user and when one is selected set the Id for the page to Id.
我需要向用户显示CompanyName,当选择一个时,将页面的Id设置为Id。
I have put a simple model together to store the information: 我把一个简单的模型放在一起来存储信息:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Portal.Models
{
public class CompanyListId
{
public int Id { get; set; }
public string CompanyName { get; set; }
}
public class CompanyListIdDBContext : DbContext
{
public DbSet<CompanyListId> Contacts { get; set; }
}
}
But I am having an issue with the controller and razor call in the view. 但我在视图中遇到了控制器和剃须刀调用的问题。
My controller: 我的控制器:
public void CompanyListIdSearch()
{
using (var dc = new CompanyListIdDBContext())
{
var content = from p in db.Companies
select new { p.CoId, p.CompanyName };
}
}
Razor call in View: Razor在视图中调用:
<div id="partners" class="linen">
@{Html.DropDownList("CompanyListIdSearch", new SelectList(Model.CompanyName));}
</div>
Grabbing the model at the top of a view @model Portal.Models.CompanyListId
在视图
@model Portal.Models.CompanyListId
的顶部@model Portal.Models.CompanyListId
So at a high level what I am trying to do is have a view that calls the controller with a razor tag, the controller updates the model with data and then that data is displayed in the drop down. 所以在高级别我想要做的是有一个用剃刀标签调用控制器的视图,控制器用数据更新模型,然后在下拉列表中显示该数据。 Am I approaching this the wrong way?
我是以错误的方式接近这个吗?
I am getting this error: 我收到此错误:
The model item passed into the dictionary is of type 'Portal.Models.DashboardContents', but this dictionary requires a model item of type 'Portal.Models.CompanyListId'.
Not quite. 不完全的。 You can't call a method in
DropDownList
like that. 你不能像这样调用
DropDownList
的方法。
Firstly, put that collection of items in your ViewBag
: 首先,将这些项目集合放在
ViewBag
:
using (var dc = new CompanyListIdDBContext())
{
var content = from p in db.Companies
select new { p.CoId, p.CompanyName };
ViewBag.CompaniesList = content
.Select(c => new SelectListItem
{
Text = c.CompanyName,
Value = c.CoId.ToString()
}).ToList();
}
Make sure you call that function in your action result before returning the view. 确保在返回视图之前在操作结果中调用该函数。
Lastly, just use your model property and switch to DropDownListFor
: 最后,只需使用您的模型属性并切换到
DropDownListFor
:
@Html.DropDownListFor(m => m.Id, ViewBag.CompaniesList);
A better approach however , is to not use the ViewBag
and to use a view model instead. 但是 , 更好的方法是不使用
ViewBag
并使用视图模型。 Something like this: 像这样的东西:
public class CompanyViewModel
{
public int Id { get; set; }
public string CompanyName { get; set; }
public List<SelectListItem> CompaniesList { get; set; }
}
Then your action method would be something like: 然后你的行动方法将是这样的:
public ActionResult YourAction()
{
var model = new CompanyViewModel();
using (var dc = new CompanyListIdDBContext())
{
var content = from p in db.Companies
select new { p.CoId, p.CompanyName };
model.CompaniesList = content
.Select(c => new SelectListItem
{
Text = c.CompanyName,
Value = c.CoId.ToString()
}).ToList();
}
return View(model);
}
Then your view would use the new model: 然后您的视图将使用新模型:
@model CompanyViewModel
Then your dropdown would be: 然后你的下拉列表是:
@Html.DropDownListFor(m => m.Id, Model.CompaniesList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.