如何包含另一个表以在asp.net mvc的视图中查看其数据
我有这个密码-如何包含另一个表以在asp.net mvc的视图中查看其数据,asp.net,model-view-controller,Asp.net,Model View Controller,我有这个密码- var add = (from h in db.Hotels where h.Address.Contains(hotels.Address) select h).Take(2); ViewBag.Related = add; <img src="~/img/@item.FirstOrDefault().Image" /> 现在,在视图中,我想显示图像,所以我使用以下代码- var add = (from h in d
var add = (from h in db.Hotels
where h.Address.Contains(hotels.Address)
select h).Take(2);
ViewBag.Related = add;
<img src="~/img/@item.FirstOrDefault().Image" />
现在,在视图中,我想显示图像,所以我使用以下代码-
var add = (from h in db.Hotels
where h.Address.Contains(hotels.Address)
select h).Take(2);
ViewBag.Related = add;
<img src="~/img/@item.FirstOrDefault().Image" />
我也尝试过使用“连接”,但出现了相同的错误。请帮帮我(
我的酒店类-
public class Hotels
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Address { get; set; }
[StringLength(8)]
public string PinCode { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string FilledBy { get; set; }
public DateTime DateAdded { get; set; }
//public int ImageId { get; set; }
public int TotalRooms { get; set; }
public bool Available { get; set; }
public virtual ICollection <Rooms> Rooms { get; set; }
public virtual ICollection <Images> Images { get; set; }
public virtual ICollection<Ameneties> Ameneties { get; set; }
public virtual ICollection <Bookings> Bookings { get; set; }
public virtual ICollection<NearByLocations> Nearby { get; set; }
public virtual ICollection<Ratings> Ratings { get; set; }
public virtual ICollection<RoomType> RoomTypes { get; set; }
public virtual ICollection<CustomerReviews> Reviews { get; set; }
public virtual ICollection<HotelRules> HotelRules { get; set; }
}
public class Images
{
[ScaffoldColumn(false)]
public int id { get; set; }
public string Image { get; set; }
public int? HotelId { get; set; }
public virtual Hotels Hotels { get; set; }
//public ICollection<Hotels> Hotels { get; set; }
}
公共级酒店
{
[脚手架立柱(假)]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串地址{get;set;}
[第(8)款]
公共字符串PinCode{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
由{get;set;}填充的公共字符串
public DateTime DateAdded{get;set;}
//公共int-ImageId{get;set;}
公共整数{get;set;}
公共bool可用{get;set;}
公共虚拟ICollection房间{get;set;}
公共虚拟ICollection映像{get;set;}
公共虚拟ICollection修正{get;set;}
公共虚拟ICollection预订{get;set;}
{get;set;}附近的公共虚拟ICollection
公共虚拟ICollection分级{get;set;}
公共虚拟ICollection RoomTypes{get;set;}
公共虚拟ICollection审阅{get;set;}
公共虚拟ICollection酒店规则{get;set;}
}
我的影像课-
public class Hotels
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Address { get; set; }
[StringLength(8)]
public string PinCode { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string FilledBy { get; set; }
public DateTime DateAdded { get; set; }
//public int ImageId { get; set; }
public int TotalRooms { get; set; }
public bool Available { get; set; }
public virtual ICollection <Rooms> Rooms { get; set; }
public virtual ICollection <Images> Images { get; set; }
public virtual ICollection<Ameneties> Ameneties { get; set; }
public virtual ICollection <Bookings> Bookings { get; set; }
public virtual ICollection<NearByLocations> Nearby { get; set; }
public virtual ICollection<Ratings> Ratings { get; set; }
public virtual ICollection<RoomType> RoomTypes { get; set; }
public virtual ICollection<CustomerReviews> Reviews { get; set; }
public virtual ICollection<HotelRules> HotelRules { get; set; }
}
public class Images
{
[ScaffoldColumn(false)]
public int id { get; set; }
public string Image { get; set; }
public int? HotelId { get; set; }
public virtual Hotels Hotels { get; set; }
//public ICollection<Hotels> Hotels { get; set; }
}
公共类图像
{
[脚手架立柱(假)]
公共int id{get;set;}
公共字符串图像{get;set;}
公共int?HotelId{get;set;}
公共虚拟酒店酒店{get;set;}
//公共ICollection酒店{get;set;}
}
我使用过这种类型的集合
这是我的详细信息视图控制器代码-
public ActionResult Details(int? id)
{
IEnumerable<Images> galleries = (from gallery in db.Images
where gallery.Hotels.Id == id
select gallery);
ViewBag.Images = galleries;
ViewBag.ImgCount = galleries.Count();
IEnumerable<Ameneties> ameneties = (from a in db.Ameneties
where a.Hotels.Id == id
select a);
ViewBag.Ameneties = ameneties;
IQueryable<Rooms> rooms = (from room in db.Rooms
where room.Hotels.Id == id
select room);
var ratings = (from rating in db.Ratings
where rating.Hotels.Id == id
select rating.Points);
ViewBag.Ratings = ratings;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Hotels hotels = db.Hotels.Find(id);
if (hotels == null)
{
return HttpNotFound();
}
var add = db.Hotels.Include("Images").Where(h => h.Address.Contains(hotels.Address))
.Select(h => h)
.Take(2)
.ToList();
var model = new MyViewModel { Hotels = add };
ViewBag.Reviews = hotels.Reviews;
ViewBag.Ratings = hotels.Ratings;
ViewBag.NearBy = hotels.Nearby;
ViewBag.RoomTypes = hotels.RoomTypes;
ViewBag.Rules = hotels.HotelRules;
return View(hotels);
}
public ActionResult详细信息(int?id)
{
IEnumerable galleries=(来自数据库中的gallery.Images
其中gallery.Hotels.Id==Id
选择画廊);
ViewBag.Images=图库;
ViewBag.ImgCount=galleries.Count();
IEnumerable修正=(从数据库修正中的a开始)
其中a.Hotels.Id==Id
选择a);
ViewBag.amenties=amenties;
IQueryable房间=(从数据库房间中的房间)
where room.Hotels.Id==Id
选择房间);
var额定值=(来自额定值,单位为db.额定值
其中rating.Hotels.Id==Id
选择评级(分数);
收视率=收视率;
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Hotels Hotels=db.Hotels.Find(id);
if(hotels==null)
{
返回HttpNotFound();
}
var add=db.Hotels.Include(“图像”)。其中(h=>h.Address.Contains(Hotels.Address))
.选择(h=>h)
.采取(2)
.ToList();
var model=newmyviewmodel{Hotels=add};
ViewBag.Reviews=hotels.Reviews;
ViewBag.Ratings=hotels.Ratings;
ViewBag.nearly=hotels.nearly;
ViewBag.RoomTypes=hotels.RoomTypes;
ViewBag.Rules=hotels.HotelRules;
返回视图(酒店);
}
能否在查询末尾添加ToList()
var add = (from h in db.Hotels
where h.Address.Contains(hotels.Address)
select h).Take(2)
.ToList();
ViewBag.Related = add;
然后可以调用item.Images.FirstOrDefault()?.Image
@foreach (var item in ViewBag.Related)
{
<img src="~/img/@item.Images.FirstOrDefault().Image" />
}
看法
@model YourNameSpace.Models.MyViewModel
@foreach(模型酒店中的var项目)
{
}
模型
公共类MyViewModel
{
公共列表酒店{get;set;}
}
您是如何获得项的
?能否显示您的视图?此处-@foreach(ViewBag.Related中的var项){}item
不是集合;您可以使用item.Image
。否则,酒店和图像之间的关系是什么。我使用了外键关系。外键在带有“Hotel”的“Images”表中。您可以同时显示Hotel
类和Image
类吗?我使用了,但出现了此错误-'System.Data.Entity.DynamicProxies.Hotels_d1ee6fd2e11bd1d9436f26 fea6336cfe76f33c59111e2abc7c1bbe456ff61c23'不包含“Image”的定义您是否可以确保您有…ToList();
并像这样访问Imageitem.Images.FirstOrDefault()?.Image
?能否将强类型的ViewModel从action方法传递到view而不是ViewBag?如何实现?我是这个MVC的新手:(在使用MyViewModel
和IActionResult时出错)