Home > DeveloperSection > Forums > Add to exisiting Database instance from MVC Controller
Ashish Pandey

Total Post:128

Points:898
Posted on    November-30-2014 10:13 PM

 ASP.Net Database  Mvc  Controller 
Ratings:


 1 Reply(s)
 578  View(s)
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?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    December-01-2014 7:17 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick