在MVC中编写查询 [英] writing queries in mvc

查看:109
本文介绍了在MVC中编写查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编辑页面中,我有一个复选框列表,我可以在其中选择酒店设施.编辑后,我想检查表中是否存在该ID.我需要写查询.如何在控制器页面中实现

在编辑页面编码中是


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

推荐答案

尝试进行以下更改

try following changes

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




希望这对您有帮助...

-萨加尔·索兰基(Sagar Solanki)




Hope this will help u...

-Sagar Solanki


这篇关于在MVC中编写查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆