Liên kết 6 models vào một view, cho phép thêm và xem asp.net mvc

Chào mọi người :D.
Mình đang lập trình một project quản lý ký túc xá, đến phần hóa đơn mình muốn liên kết 6 bảng để tạo thành một phiếu hóa đơn. Model bên dưới bao gồm:

canbo.cs

public partial class CANBO
{
    public CANBO()
    {
        this.HOADONs = new HashSet<HOADON>();
    }
    public int ID_CANBO { get; set; }
    public string MACB { get; set; }
    public string TENCB { get; set; }
    public int SDT { get; set; }
    public int CMT_CCCD { get; set; }
    public string GIOITINH { get; set; }
    public string TENDANGNHAP { get; set; }
    public string MATKHAU { get; set; }
    public virtual ICollection<HOADON> HOADONs { get; set; }
}

congtodien.cs

public partial class CONGTODIEN
{
    public int ID_DIEN { get; set; }
    public int ID_HOADON { get; set; }
    public string MACONGTO { get; set; }
    public int CHISODAU { get; set; }
    public int CHISOCUOI { get; set; }
    public virtual HOADON HOADON { get; set; }
}

congtouoc.cs

public partial class CONGTONUOC
{
    public int ID_NUOC { get; set; }
    public int ID_HOADON { get; set; }
    public string MACONGTONUOC { get; set; }
    public int CHISODAU { get; set; }
    public int CHISOCUOI { get; set; }
    public virtual HOADON HOADON { get; set; }
}

dongia.cs

public partial class DONGIA
{
    public DONGIA()
    {
        this.HOADONs = new HashSet<HOADON>();
    }
    public int ID_DONGIA { get; set; }
    public string MADONGIA { get; set; }
    public double DONGIADIEN { get; set; }
    public double DONGIANUOC { get; set; }
    public System.DateTime NGAYTHAYDOI { get; set; }
    public System.DateTime NGAYAPDUNG { get; set; }
    public virtual ICollection<HOADON> HOADONs { get; set; }
}

hoadon.cs

public partial class HOADON
{
    public HOADON()
    {
        this.CONGTODIENs = new HashSet<CONGTODIEN>();
        this.CONGTONUOCs = new HashSet<CONGTONUOC>();
    }
    public int ID_HOADON { get; set; }
    public int ID_CANBO { get; set; }
    public int ID_PHONG { get; set; }
    public int ID_DONGIA { get; set; }
    public int MAHD { get; set; }
    public Nullable<System.DateTime> NGAYLAP { get; set; }
    public Nullable<System.DateTime> THANGGHISO { get; set; }
    public virtual CANBO CANBO { get; set; }
    public virtual ICollection<CONGTODIEN> CONGTODIENs { get; set; }
    public virtual ICollection<CONGTONUOC> CONGTONUOCs { get; set; }
    public virtual DONGIA DONGIA { get; set; }
    public virtual PHONG PHONG { get; set; }
}

phong.cs

 public partial class PHONG
{
    public PHONG()
    {
        this.HOADONs = new HashSet<HOADON>();
        this.HOTROes = new HashSet<HOTRO>();
        this.LICH_SU = new HashSet<LICH_SU>();
    }
    public int ID_PHONG { get; set; }
    public string MAPHONG { get; set; }
    public string DAYPHONG { get; set; }
    public Nullable<int> SOLUONGNV { get; set; }
    public string TINHTRANG { get; set; }
    public string MOTAKHAC { get; set; }
    public string TENDANGNHAP { get; set; }
    public string MATKHAU { get; set; }
    public virtual ICollection<HOADON> HOADONs { get; set; }
    public virtual ICollection<HOTRO> HOTROes { get; set; }
    public virtual ICollection<LICH_SU> LICH_SU { get; set; }
}

Mình tạo một model để lấy những thuộc tính cần lấy từ model phía trên

phieuhoadon.cs

public partial class PhieuHoadon
{
    public int ID  { get; set; }
    public string MAPHONG { get; set; }
    public int MAHD { get; set; }
    public string MACB { get; set; }
    public Nullable<System.DateTime> NGAYLAP { get; set; }
    public Nullable<System.DateTime> THANGGHISO { get; set; }
    public string MADONGIA { get; set; }
    public int DIEN_CHISODAU { get; set; }
    public int DIEN_CHISOCUOI { get; set; }
    public int NUOC_CHISODAU { get; set; }
    public int NUOC_CHISOCUOI { get; set; }
    public double TIENDIEN { get; set; }
    public double TIENNUOC { get; set; }
}

Mỗi Model mình có tạo controller cho phép xem/thêm/sửa/xóa. Thay vì vào mỗi model để điền thông tin để tính tiền điện nước mỗi tháng (tránh rắc rối vì phải vào 6 model để điền thông tin) thì mình muốn gộp 6 bảng lại với nhau để điền nhưng mình không biết tạo controller của phieuhoadon.cs và tạo view cho nó. Bác nào biết xin hãy cho mình biết với ạ, cảm ơn rất nhiều.

Gom 6 cái đó vào 1 cái chung rồi tạo model.

public class CommonModel{
    public CANBO CanBo{get;set;} = new CANBO();
    public CONGTODIEN CongToDien{get;set;} = new CONGTODIEN();
    public CONGTONUOC CongToNuoc{get;set;} = new CONGTONUOC();
    public DONGIA DonGia{get;set;} = new DONGIA();
    public HOADON HoaDon{get;set;} = new HOADON();
    public PHONG Phong{get;set;} = new PHONG();
}

cshtml

@model {Tên Namespace}.CommonModel;

5 Likes

Tui áp dụng cái của bác với thêm các thuộc tính cần dùng nên sử dụng được rồi bác, thanks.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?