Asp.net mvc MVC4如何在视图中显示外键相关数据?
我有两个由EF 6从数据库生成的类:Asp.net mvc MVC4如何在视图中显示外键相关数据?,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我有两个由EF 6从数据库生成的类: class Person { public int PID { set; get; } public string Name { set; get; } public int CountryID { set; get; } public virtual ICollection<Country> Country { set; get;} } class Country { public int CID {
class Person {
public int PID { set; get; }
public string Name { set; get; }
public int CountryID { set; get; }
public virtual ICollection<Country> Country { set; get;}
}
class Country {
public int CID { set; get; }
public string Name { set; get; }
}
在我的数据库中,CountryID是Country.CID的外键。我想显示数据并允许在线编辑和/或删除。必须禁用这些列,但在编辑模式下,当然会启用这些列。我在谷歌上搜索了很多,也阅读了stackoverflow,但没有找到全面的帮助 有没有人能举例说明如何使用gridview?
非常感谢 您还可以找到一些用于内联编辑网格的优秀nuget软件包。您可以尝试以下方法: 控制器
public ActionResult Index()
{
var persons = db.Person.Include(c => c.Country);
return View(persons.ToList());
}
看法
@foreach(模型中的变量项){
@DisplayFor(modelItem=>item.PersonName)
@DisplayFor(modelItem=>item.Country.CountryName)
@ActionLink(“编辑”,“编辑”,新的{id=item.PersonID})|
@ActionLink(“删除”,“删除”,新的{id=item.PersonID})
public ActionResult Index()
{
var persons = db.Person.Include(c => c.Country);
return View(persons.ToList());
}
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.PersonName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country.CountryName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.PersonID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.PersonID })
</td>
</tr>