使用LinQ从两个表(视图和列表)中获取数据并返回到一个视图(代码MVC-C#)

时间:2012-05-13 09:52:26

标签: asp.net-mvc

我有两个表:tour和hotel我想用join执行查询并在View中获得结果。

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

enter link description here

控制器中的

我有这个代码:

 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;
    }

希望得到大家的帮助............谢谢

1 个答案:

答案 0 :(得分:0)

当您使用MVC时,我强烈建议您使用实体框架。如果您从未使用它,请查看:http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=aspdotnet-mvc3-intro - 非常好的视频教程。 我认为Tour - Hotel关系是典型的多对多关系。由EF class Tour映射将具有属性Hotels,反之亦然。如果您传递Tour以查看@Model.Hotels,请向您汇总相关酒店。 BTW做了一些重构代码,请:)