如何在视图中显示来自其他模型的相关数据。我错过了什么?

时间:2015-12-01 17:26:28

标签: asp.net-mvc

我正在为房地产做画廊。我在名为" Imot"的表中保存数据。和其他信息,如名为" Region"的表中的区域。我试着在" Imot"中显示所有项目。并在视图中的每个div中添加一些其他信息。例如:我的桌子" Imot"我有imot.ID = 1和imot.RegionID = 2。在视图中我必须看到该房地产的区域是Sofia,它保存在区域中,区域ID为区域ID = 2。 为这个主题写了很多,但没有什么对我有用。 这是我的代码:

型号:

public class Imot
{
public int ID { get; set; }
public string ShortName { get; set; }
public string LongName { get; set; }
public int VDID { get; set; }
public int VOID { get; set; }
public int OblastID { get; set; }
public int ObshtinaID { get; set; }
public int VillageID { get; set; }
public int RegionID { get; set; }
public int TypeOfConID { get; set; }
public int StatusImotID { get; set; }
public string MainImage { get; set; }
public int Price { get; set; }
public int Square { get; set; }
public int Floor { get; set; }
public virtual VD VD { get; set; }
public virtual VO VO { get; set; }
public virtual Oblast Oblast { get; set; }
public virtual Obshtina Obshtina { get; set; }
public virtual Village Village { get; set; }
public virtual Region Region { get; set; }
public virtual TypeOfCon TypeOfCon { get; set; }
public virtual StatusImot StatusImot { get; set; }
public virtual ICollection<MyImages> MyImages { get; set; }
}

public class Region
{
public int RegionID { get; set; }
public string RegionName { get; set; }
public virtual ICollection<Imot> Imots { get; set; }
}

上下文:

public class MyProjectContext: DbContext
{   
public MyProjectContext() : base("MyProjectContext") {}
public DbSet<VD> VDs { get; set; }
public DbSet<VO> VOs { get; set; }
public DbSet<Oblast> Oblasts { get; set; }
public DbSet<Obshtina> Obshtinas { get; set; }
public DbSet<Village> Villages { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<TypeOfCon> TypeOfCons { get; set; }
public DbSet<StatusImot> StatusImots { get; set; }
public DbSet<Imot> Imots { get; set; }
public DbSet<MyImages> MyImageses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}}

种子:

public class MyProjectInitializer :System.Data.Entity.
DropCreateDatabaseIfModelChanges<MyProjectContext>
{protected override void Seed(MyProjectContext context){
var Regions = new List<Region>
{
new Region{RegionName="Sofia"},
};
var Imots = new List<Imot> 
{
new Imot  {ShortName="",LongName="",VDID=1,VOID=5,OblastID=1, 
ObshtinaID=1,VillageID=1,RegionID=4,TypeOfConID=1,
StatusImotID=1,MainImage="",Price=10000,Square=100,Floor=6}
};}}

控制器:

public class SalesController : Controller
{
private MyProjectContext db = new MyProjectContext();
// GET: ImotsAdmin
public ActionResult Index()
{
ViewBag.RegionID = new SelectList(db.Regions, "RegionID", "RegionName");
var imots = db.Imots.Include(i => i.Oblast).Include(i => i.Obshtina)
.Include(i => i.Region).Include(i => i.StatusImot).Include(i =>i
.TypeOfCon).Include(i => i.VD).Include(i => i.Village).Include(i => i.VO);
 return View(imots.ToList());
 }
 }

查看:

@model IEnumerable<MyProject.Models.Imot>
@using MyProject.Models
@{ViewBag.Title = "For Sale";}
@using (Html.BeginForm("Index", "Sales", FormMethod.Get))
{
@Html.AntiForgeryToken()
@Html.DropDownList("RegionID", (SelectList)ViewBag.RegionId, "Region",
new {  @class = "form-control" })
<input type="submit" value="Search" class="btn btn-primary" />
}
<div>
@foreach (var item in Model)
{
<div class="img">
<div class="desc">@item.Price</div>
<a target="_blank" href='@Url.Action("Details", "Sales", new {id=item.ID})'>
<img src="@item.MainImage" alt="Имот" width="110" height="90"></a>
<div class="desc">@item.RegionID</div>
<div class="desc">@item.OblastID , @item.Square , @item.Floor </div>
<div class="desc">@item.ShortName</div>
@Html.ActionLink("More info...", "Details", new { id = item.ID }) |
</div>
}
</div>

以下是我在View中获得的内容的链接:http://prntscr.com/995gio &#34;地区旁边:&#34;必须是地区的名称:&#34; Sofiq&#34;但只是ID。 谢谢:))

1 个答案:

答案 0 :(得分:0)

您正在RegionID对象上打印Imot属性。如果要打印区域名称,则应访问Region导航属性并访问其RegionName属性。

所以在你的foreach循环中,替换

<div class="desc">@item.RegionID</div>

使用

<div class="desc">@item.Region.RegionName </div>