Gọi data từ CSDL để hiển thị trong Index sau khi đã ghép 3 model nhập dữ liệu asp mvc 5

Tui đã ghép 3 model này vào 1 class để nhập dữ liệu cùng một lúc.

model công tơ điện

public partial class CONGTODIEN
{
    [Key]
    public int ID_DIEN { get; set; }
    public int ID_PHONG { get; set; }
    public int CHISODAU { get; set; }
    public int CHISOCUOI { get; set; }
    public int? THANG { get; set; }
    public int? NAM { get; set; }
    public int? TRANGTHAI { get; set; }
    public virtual PHONG PHONG { get; set; }
}

model công tơ nước

public partial class CONGTONUOC
{
    [Key]
    public int ID_NUOC { get; set; }
    public int ID_PHONG { get; set; }
    public int CHISODAU { get; set; }
    public int CHISOCUOI { get; set; }
    public int? THANG { get; set; }
    public int? NAM { get; set; }
    public int? TRANGTHAI { get; set; }
    public virtual PHONG PHONG { get; set; }
}

model phòng

public partial class PHONG
{
    public PHONG()
    {
        CONGTODIENs = new HashSet<CONGTODIEN>();
        CONGTONUOCs = new HashSet<CONGTONUOC>();
        HOADONs = new HashSet<HOADON>();
        HOTROes = new HashSet<HOTRO>();
        LICH_SU = new HashSet<LICH_SU>();
    }
    public int ID_PHONG { get; set; }
    public int ID_DAY { get; set; }
    [Required]
    [StringLength(10)]
    public string MAPHONG { get; set; }
    public int? SOLUONGNV { get; set; }
    [StringLength(50)]
    public string TINHTRANG { get; set; }
    [StringLength(80)]
    public string MOTAKHAC { get; set; }
    [StringLength(50)]
    public string TENDANGNHAP { get; set; }
    [StringLength(50)]
    public string MATKHAU { get; set; }
    public bool? TRANGTHAI { get; set; }
    public virtual ICollection<CONGTODIEN> CONGTODIENs { get; set; }
    public virtual ICollection<CONGTONUOC> CONGTONUOCs { get; set; }
    public virtual DAYPHONG DAYPHONG { 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; }
}

Hóa đơn view model (class ghép thuộc tính từ 3 model)

public class ViewModel_HoaDon : IEnumerable
{
    // Chưa sử dụng
    public int ID_DONGIA { get; set; }
    public int MADONGIA { get; set; }
    public double DONGIADIEN { get; set; }
    public double DONGIANUOC { get; set; }
    public bool? TRANGTHAI_DONGIA { get; set; }
    public DateTime? NGAYAPDUNG { get; set; }
    public int ID_CANBO { get; set; }
    public int MAHD { get; set; }
    public string TRANGTHAI_HOADON { get; set; }
    public int? THANG_HOADON { get; set; }
    public int? NAM_HOADON { get; set; }

    // Đang sử dụng
    public int THANG { get; set; }
    public int NAM { get; set; }
    public int TRANG_THAI { get; set; }
    public int ID_PHONG { get; set; }
    public int NUOC_CHISODAU { get; set; }
    public int NUOC_CHISOCUOI { get; set; }
    public int DIEN_CHISODAU { get; set; }
    public int DIEN_CHISOCUOI { get; set; }
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

Controller của ViewModel_HoaDon

public class ViewModelHoaDonController : Controller
{
    private myDB db = new myDB();
    // GET: ViewModelHoaDon
    public ActionResult Index()
    {
        var model = new ViewModel_HoaDon();
        return View(model);
    }
    // Tạo hóa đơn cho các phòng đang sử dụng
    [HttpPost]
    public ActionResult Create()
    {
        dynamic listPhong = (from p in db.PHONGs where p.TRANGTHAI == true select p.ID_PHONG).ToList();
        foreach (var phong in listPhong)
        {
            HOADON data = new HOADON()
            {
                ID_PHONG = phong.ID_PHONG,
                THANG = 1,
                NAM = 1,
                //ID_CANBO = int.Parse(Session["ID"].ToString())
            };
            db.HOADONs.Add(data);
        }
        db.SaveChanges();
        return View(new ViewModel_HoaDon());
    }

    [HttpGet]
    public ActionResult CreateDIEN_NUOC()
    {
        return View();
    }
   // Điền chỉ số điện và nước
    [HttpPost]
    public ActionResult CreateDIEN_NUOC(ViewModel_HoaDon model)
    {
        CONGTONUOC nuoc = new CONGTONUOC()
        {
            ID_PHONG = model.ID_PHONG,
            THANG = model.THANG,
            NAM = model.NAM,
            TRANGTHAI = 1,
            CHISODAU = model.NUOC_CHISODAU,
            CHISOCUOI = model.NUOC_CHISOCUOI
        };
        db.CONGTONUOCs.Add(nuoc);

        // dien
        CONGTODIEN dien = new CONGTODIEN()
        {
            ID_PHONG = model.ID_PHONG,
            THANG = model.THANG,
            NAM = model.NAM,
            TRANGTHAI = 1,
            CHISODAU = model.DIEN_CHISODAU,
            CHISOCUOI = model.DIEN_CHISOCUOI
        };
        db.CONGTODIENs.Add(dien);
        db.SaveChanges();
        return View(model);
    }
}

Index của ViewModel_HoaDon

@model HoaDon02.Models.ViewModel_HoaDon
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ID_PHONG)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DIEN_CHISODAU)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DIEN_CHISOCUOI)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.NUOC_CHISODAU)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.NUOC_CHISOCUOI)
    </th>
    
    <th>
        @Html.DisplayNameFor(model => model.THANG)
    </th>
    
    <th>
        @Html.DisplayNameFor(model => model.NAM)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.PHONG.MAPHONG)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CHISODAU)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CHISOCUOI)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.THANG)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NAM)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID_DIEN }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID_DIEN }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID_DIEN })
    </td>
</tr>
}
</table>

Các thông tin chỉ số đã điền thành công tại view Create và lưu trong CSDL, khi cho hiển thị tại index thì bị lỗi

  • Khi model không để IEnumerable vào @model HoaDon02.Models.ViewModel_HoaDon thì nó báo lỗi như này:
    image

  • Khi mình để IEnumberable thì tại foreach nó không báo lỗi nữa, tuy nhiên về phần controller thì lại báo lỗi

    public ActionResult Index()
    {
        var model = new ViewModel_HoaDon();
        return View(model); // xuất hiện lỗi tại model
    }
    

Có cách nào để gọi dữ liệu lên Index không mấy bác?

bạn muốn mọi người chạy những đoạn code của bạn bằng mắt hay sao, ngay cả thông báo lỗi gì bạn cũng không show lên đây?

Xin lỗi bác, lúc mình tạo bài thì nó báo thành viên mới chỉ được tải một ảnh khi đăng bài. Nên mình sửa bài lại rồi.

nó đã ghi rất rõ ràng, bạn cần truyền vào Inumerable nhưng bạn chỉ truyền vào object
view của bạn mong muốn nhận vào cái gì, bạn đang truyền vào cái gì
cả 2 chỗ đều do bạn viết mà nó lại không khớp?

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