コード例 #1
0
        public ActionResult Create(FormCollection collection)
        {
            if (Session["LogedUserID"] != null)
            {
                var cityname = "";
                var cityid   = "";
                ViewBag.successMessage = "";
                ViewBag.errorMessage   = "";
                try
                {
                    var action = Request.Form["action"].ToString();
                    if (action == "create")
                    {
                        cityname = Request.Form["cityname"].ToString();
                        var city = (
                            from p in db.Cities
                            where (p.cityName == cityname && p.active == true)
                            select p
                            ).FirstOrDefault();
                        if (city != null)
                        {
                            ViewBag.successMessage = "";
                            ViewBag.errorMessage   = "City already exists";
                        }
                        if (city == null)
                        {
                            db.SP_AddCity(cityname, Session["LogedUserID"].ToString());
                            db.SaveChanges();
                            ViewBag.successMessage = "Record has been saved successfully";
                            ViewBag.errorMessage   = "";
                        }
                    }
                    if (action == "edit")
                    {
                        cityid   = Request.Form["id"].ToString();
                        cityname = Request.Form["cityname"].ToString();
                        //var city = (
                        //               from p in db.City
                        //               where (p.cityName == cityname && p.active==true)
                        //               select p
                        //           ).FirstOrDefault();
                        //if (city != null)
                        //{
                        //    ViewBag.successMessage = "";
                        //    ViewBag.errorMessage = "City already exists";

                        //}
                        //if (city == null)
                        //{
                        db.sp_UpdateCity(Convert.ToInt64(cityid), cityname, Session["LogedUserID"].ToString(), System.DateTime.Now);
                        db.SaveChanges();
                        ViewBag.successMessage = "Record has been saved successfully";
                        ViewBag.errorMessage   = "";
                        //}
                    }
                    if (action == "delete")
                    {
                        cityid = Request.Form["id"].ToString();
                        db.sp_DeleteCity(Convert.ToInt64(cityid), Session["LogedUserID"].ToString(), System.DateTime.Now);
                        db.SaveChanges();
                        ViewBag.successMessage = "Record has been deleted successfully";
                        ViewBag.errorMessage   = "";
                    }
                    var __existingcityList = db.SP_SelectCity();
                    return(View(__existingcityList));
                }
                catch (Exception ex)
                {
                    ViewBag.errorMessage = "Error occurred while processing your request.";
                    var _existingcityList = db.SP_SelectCity();
                    return(View(_existingcityList));
                }
            }
            else
            {
                return(RedirectToAction("AdminLogin", "Account"));
            }
        }