我的控制器中有以下代码:
public ViewResult Index()
{
var q = from ci in db.City
join co in db.Country on ci.CityID equals co.CityID
select ci;
return View(q);
}
Run Code Online (Sandbox Code Playgroud)
数据库如下:
Table City: CityID, CityName
Table Country: CountryID, CountryName
Run Code Online (Sandbox Code Playgroud)
在索引视图中如何显示CityName和CountryName:
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.CityName)
Run Code Online (Sandbox Code Playgroud)
我无法让它显示CountryName :(
谢谢!
它不起作用,因为您只在查询中选择城市.你需要选择两者.使用匿名对象是一种方式:
select new { City = ci, Country = co }
Run Code Online (Sandbox Code Playgroud)
然后,您应该能够访问item.City.Cityname,item.Country.CountryName等等.
| 归档时间: |
|
| 查看次数: |
241 次 |
| 最近记录: |