Home > Enterprise >  Insert multiple record into one model with condition ASP.NET MVC
Insert multiple record into one model with condition ASP.NET MVC

Time:07-08

I'm trying to insert multiple record into database with loop, it's really pain for me when insert bills record for each room in used. So my goal is to create bill for all rooms in used TRANGTHAI == 1 by using form.

Here's my controller:

    [HttpPost]
    public ActionResult Create(HOADON hoadon)
    {
        if (ModelState.IsValid)
        {
            dynamic listPhong = (from p in db.PHONGs where p.TRANGTHAI == 1 select p.ID_PHONG).ToList();
            foreach (var phong in listPhong)
            {
                hoadon = new HOADON()
                {
                    ID_PHONG = phong.ID_PHONG, // cant get ID here
                    ID_DONGIA = 1,
                    TRANGTHAI = "Chua dong tien" // status = "Not paid"
                    ID_CANBO = int.Parse(Session["ID"].ToString()) // get ID manager who creates this bill
                };
                db.HOADONs.Add(hoadon);
                return RedirectToAction("Index");
            }
            db.SaveChanges();
            return View(hoadon);
    }

I keep the month and year in the Create.cshtml so manager just need to type 2 of them and the rest is auto generated.

If I putID_PHONG = 1 in controller, it works but get the data of current room that has ID_PHONG = 1.

When I put ID_PHONG = phong.ID_PHONG (code suggestion doesn't suggest to me) and run the project, it throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''int' does not contain a definition for 'ID_PHONG'' and This exception was originally thrown at this call stack: HoaDon02.Controllers.HOADONsController.Create(HoaDon02.Models.HOADON) in HOADONsController.cs [External Code]

I also tried put these two model into view model (get, set) and call it like this

 public ActionResult Create(ViewModel_HoaDon vm)
    {

        var id_phong = (vm.Phong.ID_PHONG);
        if (ModelState.IsValid)
        {
            dynamic listPhong = (from p in id_phong where p.TRANGTHAI == 1 select p.ID_PHONG).ToList(); // red line in id_phong and where.
            foreach (var phong in listPhong)
            {
                HOADON data = new HOADON()
                {
                    ID_PHONG = phong.ID_PHONG,
                    ID_DONGIA = 1,
                    THANG = 1,
                    NAM = DateTime.Now.Year,
                    ID_CANBO = int.Parse(Session["ID"].ToString())
                };
                db.HOADONs.Add(data);
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vm);
    }

The error it throws is: Could not find an implementation of the query pattern for source type 'int'. 'Where' not found.

Please let me know if you have solutions. Thanks a lot.

CodePudding user response:

Thanks to Chetan, I have two solution for this. You can use dynamic or List<int> and in

HOADON data = new HOADON()
            {
                ID_PHONG = phong.ID_PHONG,
                ID_DONGIA = 1,
                THANG = 1,
                NAM = DateTime.Now.Year,
                ID_CANBO = int.Parse(Session["ID"].ToString())
            };

you just simplify to ID_PHONG = phong

  • Related