ADD TO EXISITING DATABASE INSTANCE FROM MVC CONTROLLER

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
 909  View(s)
Ratings:
Rate this:

Private BookingDB db = new BookingDB();

        Private MonthDb mdb = new MonthDB();

 

        if (ModelState.IsValid)

        {

            String date = (booking.Start_Date).ToString();

 

            var check = from b in mdb.months

                        where b.BookedDays.Contains(date)

                        select b;

 

            if (check != null)

            {

                return View(booking);

            }

            else

            {

                booking.Reservation_Owner = User.Identity.Name;

 

                //Add booking.Start_Date to mdb.Entry(check).BookedDays

 

                mdb.SaveChanges();

                db.bookings.Add(booking);

                db.SaveChanges();

                return RedirectToAction("Index");

            }

        }

I've got this code that on creation of a new booking, will check that no exisiting bookings have already been made on or around that specific day. if the day to be booked is not already been booked (ie exists under BookedDays in mdb.months) then i wish to add the Start_Date of the booking, to the BookedDays string in the mdb.months database (the mdb.month database is just a list of the 12 months)

at first i tried using mdb.Entry() to add to that specific month instance, however i cannot get it to work.

 

the error is:

the model does not have a definition for BookedDays

what to do?

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: Add to exisiting Database instance from MVC Controller

    Your checking that check is null

     

    if (check != null)

    {

         return View(booking);

    }

    else

    {

    and then using check anyway:

     

    check.BookedDays

    check is null and therefore does not contain any BookedDays. I'm guessing your null check is the wrong way around and should be

     

    if (check == null)

    {

         return View(booking);

    }

    else

    {

    That said your problem is not well explained so I'm not sure.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!