0

我有两个表:旅游和酒店我想用 join 执行查询并在视图中获取结果。

如何查看两个表中的数据,如下图?

在此处输入链接描述

在控制器中我有这个代码:

 public ActionResult DetailView(string strID)
    {
        var id_tour =  (from data1 in _db.Tours
                       join dataview2 in _db.TypeOfCosts on data1.ID_TourCost equals dataview2.ID_TourCost
                       where (data1.ID_Tour == strID) && (data1.ID_TourCost == dataview2.ID_TourCost)
                       select new
                       {
                           data1.TourName,
                           data1.ID_Tour,
                           data1.DepartureDay,
                           data1.DeparturePosition,
                           data1.AvailableRoom, 
                           dataview2.AdultCost,
                           dataview2.ChildrenCost,
                           dataview2.BabyCost,
                       }).FirstOrDefault();
        var view_tour = new DetailModels(id_tour.TourName, id_tour.ID_Tour, Convert.ToDateTime(id_tour.DepartureDay), id_tour.DeparturePosition,
            Convert.ToInt32(id_tour.AvailableRoom),
            Convert.ToInt32(id_tour.AdultCost), Convert.ToInt32(id_tour.ChildrenCost), Convert.ToInt32(id_tour.BabyCost));
        return View(view_tour);
    }
    [HttpPost]
    public ActionResult DetailView(DetailModels model)
    {
        var id_hotel = from data2 in _db.Tours
                       join dataview3 in _db.TourPrograms on data2.ID_Tour equals dataview3.ID_Tour
                       join dataview4 in _db.Programs on dataview3.ID_TourProgram equals dataview4.ID_TourProgram
                       join dataview5 in _db.Hotels on dataview4.ID_Hotel equals dataview5.ID_Hotel
                       where (data2.ID_Tour == dataview3.ID_Tour) &&
                       (dataview3.ID_TourProgram == dataview4.ID_TourProgram) && (dataview4.ID_Hotel == dataview5.ID_Hotel)
                       select new
                       {
                           dataview5.HotelName,
                           dataview5.HotelAddress,
                           dataview5.HotelPhoneNumber,
                       };
        // chuyền dữ liệu vào như thế nào
        return RedirectToAction("DetailView", "Tourpackage");
    }

在模型中我有这个代码:

enter code here public class DetailModels
{
    public string TourName { get; set; }
    public string ID_Tour { get; set; }
    public DateTime DepartureDay { get; set; }
    public string DeparturePosition { get; set; }
    public int AvailableRoom { get; set; }
    public string HotelName { get; set; }
    public string HotelAddress { get; set; }
    public int HotelPhoneNumber { get; set; }
    public int AdultCost { get; set; }
    public int ChildrenCost { get; set; }
    public int BabyCost { get; set; }
    public DetailModels(string tourname, string idtour, DateTime dapartureday, string departureposition, int availableroom,
        int adultcost, int childrencost, int babycost)
    {
        this.TourName = tourname; this.ID_Tour = idtour; this.DepartureDay = dapartureday; this.DeparturePosition = departureposition;
        this.AvailableRoom = availableroom;
        this.AdultCost = adultcost; this.ChildrenCost = childrencost; this.BabyCost = babycost;
    }

希望对大家有所帮助............谢谢

4

1 回答 1

0

当您使用 MVC 时,我强烈建议您使用实体框架。如果您从不使用它,请查看:http ://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName= aspdotnet-mvc3-intro - 非常好的视频教程。我假设Tour-Hotel关系是典型的多对多。由 EF 类映射的 Tour 将具有属性Hotels,反之亦然。如果您通过例如Tour查看@Model.Hotels给您相关酒店的集合。顺便说一句,请做一些重构代码:)

于 2012-05-13T10:09:52.803 回答