c# – How to show data from DB to Index after merged 3 models in one view in ASP MVC 5?

I merged these 3 model to another class to insert data in one view.

congtodien.cs

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

congtonuoc.cs

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

phong.cs

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

The view model class take from 3 models above

public class ViewModel_HoaDon
{
    // Not used
    public int ID_DIEN { get; set; }
    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; }

    // In used
    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; }
}

ViewModel_HoaDonController.cs

public class ViewModelHoaDonController : Controller
{
    private myDB db = new myDB();
    // GET: ViewModelHoaDon
    public ActionResult Index()
    {
        var model = new ViewModel_HoaDon();
        return View(model);
    }

    [HttpPost]
    // Generate bill due to room in service
    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]
    // allow user type water meter and electric meter in one view
    public ActionResult CreateDIEN_NUOC()
    {
        return View();
    }
    [HttpPost]
    public ActionResult CreateDIEN_NUOC(ViewModel_HoaDon model)
    {
        model.THANG = System.DateTime.Now.Month;
        model.NAM = System.DateTime.Now.Year;
        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.cshtml

@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.ID_PHONG)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DIEN_CHISODAU)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DIEN_CHISOCUOI)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NUOC_CHISODAU)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NUOC_CHISOCUOI)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.THANG)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NAM)
    </td>
</tr>
}
</table>

The data’s saved successfully when typing in Create.cshtmlbut I cant show data to Index.cshtml from what I just typed. I tried these method but none of them work.

  • I put IEnumerable into ViewModel_HoaDon

    public class ViewModel_HoaDon : IEnumerable and the foreach in Index.cshtml doesnt get any data. It says ‘object’ does not contain a definition for ‘ID_PHONG’ and no accessible extension method ‘ID_PHONG’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)and so do the other

  • I put IEnumerable into @model HoaDon02.Models.ViewModel_HoaDon

    @model IEnumerable<HoaDon02.Models.ViewModel_HoaDon>

    the error i got the same above

  • when i casted to IEnumberable into return View(), and got :
    Unable to cast object of type ‘HoaDon02.Models.ViewModel_HoaDon’ to type ‘System.Collections.Generic.IEnumerable`1[HoaDon02.Models.ViewModel_HoaDon]

     public ActionResult Index()
     {
          var model = new ViewModel_HoaDon();
          return View((IEnumerable<ViewModel_HoaDon>)model);
     }
    
  • No IEnumerable thing, got error

Is there another way to get data to index?

Leave a Comment