Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in edit page i have a list of checkboxes where i can select hotel facilities. after editing i want to check whether the id is existing in table. i need to write query for it . how it is possible in controller page

in edit page coding is


public ActionResult Edit(Hotel hotel)
       {
           if (ModelState.IsValid)
           {
               hotel.Status = 1;
               db.Entry(hotel).State = EntityState.Modified;

                var hFacilities = Request["HotelFesilityId"].Split(',');
               foreach (var hf in hFacilities)
               {
                   var hfid = Convert.ToInt32(hf);
                   //here i want to check  hfid is exising in the table
                   var hotelfaciliti = db.HotelFesilities.Find(hfid);
                   hotel.HotelFesilities.Add(hotelfaciliti);
               }
               db.SaveChanges();
               return RedirectToAction("Index");
           }
           ViewBag.HotelCategoryHotelCategoryId = new SelectList(db.HotelCategories, "HotelCategoryId", "CategoryName", hotel.HotelCategoryHotelCategoryId);
           return View(hotel);
       }
Posted
Updated 11-Sep-11 21:47pm
Comments
Saad Bin Tahir 12-Sep-11 5:43am    
For database what you are using ? Entity framework or any other tool ?

1 solution

try following changes

C#
public ActionResult Edit(Hotel hotel)
        {
            if (ModelState.IsValid)
            {
                hotel.Status = 1;
                db.Entry(hotel).State = EntityState.Modified;

                 var hFacilities = Request["HotelFesilityId"].Split(',');
                foreach (var hf in hFacilities)
                {
                    var hfid = Convert.ToInt32(hf);
                    //here i want to check  hfid is exising in the table
                    //var hotelfaciliti = db.HotelFesilities.Find(hfid);
                    var a= db.HotelFesilities.Where(id==hfid).Count();
                    if(a>0)
                    {
                        //Do task 1
                    }
                    else
                    {
                        //Do tast for else
                    }    
                    hotel.HotelFesilities.Add(hotelfaciliti);
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.HotelCategoryHotelCategoryId = new SelectList(db.HotelCategories, "HotelCategoryId", "CategoryName", hotel.HotelCategoryHotelCategoryId);
            return View(hotel);
        }



Hope this will help u...

-Sagar Solanki
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900