ASP.NET MVC使用绑定LINQ查询结果HTML.DropDownList() [英] ASP.NET Binding linq query results to HTML.DropDownList() using MVC
问题描述
我想建立一个下拉列表中有一个单一的数据库调用。
我的表包括一个ID列和公司名称列的。我需要显示公司名称给用户,并在选中一个设置ID为页面标识。
我已经把一个简单的模型一起存储的信息:
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity的;
使用System.Linq的;
使用的System.Web;命名空间Portal.Models
{
公共类CompanyListId
{
公众诠释标识{搞定;组; }
公共字符串公司名称{搞定;组; }
} 公共类CompanyListIdDBContext:的DbContext
{
公共DbSet< CompanyListId>联系方式{搞定;组; }
}
}
不过,我有在视图控制器和剃刀通话的问题。
我的控制器:
公共无效CompanyListIdSearch()
{
使用(VAR DC =新CompanyListIdDBContext())
{
在db.Companies VAR内容=由对
选择新的{p.CoId,p.CompanyName};
}
}
在View剃刀电话:
< DIV ID =合作伙伴级=麻> @ {Html.DropDownList(CompanyListIdSearch,新的SelectList(Model.CompanyName));}
< / DIV>
在视图顶部拼抢模型 @model Portal.Models.CompanyListId
所以在较高的水平我所试图做的是有一个调用用剃刀标记的控制器视图,控制器更新的数据模型,然后这些数据显示在下拉。我在这接近走错了路?
我得到这个错误:
传递到字典的模型项的类型是Portal.Models.DashboardContents',但本词典需要类型的模型项目Portal.Models.CompanyListId。
不太。你不能调用的DropDownList
的方法那样的。
首先,把项目的该集合在 ViewBag
:
使用(VAR DC =新CompanyListIdDBContext())
{
在db.Companies VAR内容=由对
选择新的{p.CoId,p.CompanyName}; ViewBag.CompaniesList =内容
。选择(C =>新建SelectListItem
{
文= c.CompanyName,
值= c.CoId.ToString()
})了ToList()。
}
请确保您调用您的操作结果函数返回视图前。
最后,只需用你的模型属性,并切换到 DropDownListFor
:
@ Html.DropDownListFor(M = GT; m.Id,ViewBag.CompaniesList);
一个更好的办法但是,是不使用 ViewBag
,并使用视图模型来代替。事情是这样的:
公共类CompanyViewModel
{
公众诠释标识{搞定;组; }
公共字符串公司名称{搞定;组; }
公开名单< SelectListItem> CompaniesList {搞定;组; }
}
那么你的操作方法是这样的:
公众的ActionResult YourAction()
{
VAR模型=新CompanyViewModel(); 使用(VAR DC =新CompanyListIdDBContext())
{
在db.Companies VAR内容=由对
选择新的{p.CoId,p.CompanyName}; model.CompaniesList =内容
。选择(C =>新建SelectListItem
{
文= c.CompanyName,
值= c.CoId.ToString()
})了ToList()。
} 返回查看(模型);
}
那么你的观点将使用新模式:
@model CompanyViewModel
那么你的下拉菜单将是:
@ Html.DropDownListFor(M = GT; m.Id,Model.CompaniesList)
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. I need to display the CompanyName to the user and when one is selected set the Id for the page to 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:
<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
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.
Firstly, put that collection of items in your 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
:
@Html.DropDownListFor(m => m.Id, ViewBag.CompaniesList);
A better approach however, is to not use the ViewBag
and to use a view model instead. 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)
这篇关于ASP.NET MVC使用绑定LINQ查询结果HTML.DropDownList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!