Click here to Skip to main content
15,893,368 members
Articles / Web Development / ASP.NET

"How to series" about MVC, jQuery, JSON, paging, mapRoute

Rate me:
Please Sign up or sign in to vote.
4.91/5 (86 votes)
19 Apr 2013CPOL12 min read 135K   4.8K   199  
"How to series" about MVC, jQuery, JSON, paging, mapRoute.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AddressBook_mvc3_jQuery.Models;
using AddressBook_mvc3_jQuery.Data;
using AddressBook_mvc3_jQuery.Helpers;

namespace AddressBook_mvc3_jQuery.Controllers
{
    public class CountryController : Controller
    {


        public ActionResult Index()
        {
            ViewBag.Message = "";
            return View();
        }


        //public ActionResult CountryList()
        //{
        //    List<Country> list = Data.Repository.GetCountryList();
        //    return View(list);
        //}


        public JsonResult GetCountryList()
        {

            List<Country> list = Repository.GetCountryList();

            JsonResult jr = Json(new
            {
                Html = this.RenderPartialView("CountryList", list),
                Message = "OK"
            }, JsonRequestBehavior.AllowGet);

            return jr;
        }


        public JsonResult GetCityList(int countryNo)
        {
            
            List<City> list = Repository.GetCityList().Where(x=> x.CountryNo==countryNo).ToList();

            JsonResult jr = Json(new
            {
                Html = this.RenderPartialView("CityList", list),
                Message = "OK"
            }, JsonRequestBehavior.AllowGet);

            return jr;
        }



        [HttpGet]
        public ActionResult CountrySave(int countryNo)
        {
            Country country = new Country();
            country.CountryNo = 0;
            country.CountryName = "";


            if (countryNo > 0)
            {
                country = Repository.GetCountryList().Where(c => c.CountryNo == countryNo).FirstOrDefault();
            }

            return PartialView(country);
        }


    


        [HttpPost]
        public JsonResult CountrySave(Country c)
        {
            object obj = null;

            if (ModelState.IsValid)
            {
                if (c.CountryNo == 0)
                {
                    //find last record
                    //if it is database system no need to this line. Probably the CountryNo would be autoincrement
                    c.CountryNo = Data.Repository.GetCountryList().OrderBy(x => x.CountryNo).Last().CountryNo + 1;
                    Data.Repository.GetCountryList().Add(c);

                    obj = new
                    {
                        Success = true,
                        Message = "Country Saved successfully",
                        Object = c,
                        operationType = "INSERT",
                        Html = this.RenderPartialView("_countryLine", c)
                    };
                }
                else
                {
                    Country country = Repository.GetCountryList().Where(x => x.CountryNo == c.CountryNo).FirstOrDefault();
                    country.CountryName = c.CountryName;

                    obj = new
                    {
                        Success = true,
                        Message = "Saved successfully",
                        Object = country,
                        operationType = "UPDATE",
                        Html = this.RenderPartialView("_countryLine", country)
                    };
                }

            }
            else
            {
                obj = new { Success = false, Message = "Please check form" };
            }

            return Json(obj, JsonRequestBehavior.DenyGet);
        }


        //[HttpPost]
        //public JsonResult CountrySave(FormCollection fc)
        //{
        //    object obj = null;


        //    Country countryTmp = new Country();
        //    countryTmp.CountryNo = Convert.ToInt32(fc["TBCountryNo"].ToString());
        //    countryTmp.CountryName = fc["TBCountryName"].ToString();


        //    if (ModelState.IsValid)
        //    {
        //        if (countryTmp.CountryNo == 0)
        //        {
        //            //find last record 
        //            //if it is database system no need to this line. Probably the CountryNo would be autoincrement
        //            countryTmp.CountryNo = Data.Repository.GetCountryList().OrderBy(x => x.CountryNo).Last().CountryNo + 1;

        //            Data.Repository.GetCountryList().Add(countryTmp);


        //            obj = new
        //            {
        //                Success = true,
        //                Message = "Country added successfully",
        //                Object = countryTmp,
        //                operationType = "INSERT",
        //                Html = this.RenderPartialView("_countryLine", countryTmp)
        //            };
        //        }
        //        else
        //        {
        //            Country country = Repository.GetCountryList().Where(c => c.CountryNo == countryTmp.CountryNo).FirstOrDefault();
        //            country.CountryName = countryTmp.CountryName;


        //            obj = new
        //            {
        //                Success = true,
        //                Message = "Country updated successfully",
        //                Object = country,
        //                operationType = "UPDATE",
        //                Html = this.RenderPartialView("_countryLine", country)
        //            };
        //        }

        //    }
        //    else
        //    {
        //        obj = new { Success = false, Message = "Please check form" };
        //    }

        //    return Json(obj, JsonRequestBehavior.DenyGet);
        //}


        [HttpPost]
        public JsonResult DeleteCountry(int countryNo)
        {
            object obj = null;
            
            try
            {
                Country country = Data.Repository.GetCountryList().Where(c => c.CountryNo == countryNo).FirstOrDefault();
                
                if (country != null)
                {

                    int isCountryUsed = Data.Repository.GetAddressList().Where(x => x.CountryNo == country.CountryNo).Count();

                    if (isCountryUsed == 0)
                    {
                        Data.Repository.GetCountryList().Remove(country);


                        obj = new
                        {
                            Success = true,
                            Message = "Country Deleted",
                            operationType = "DELETE"
                        };
                    }
                    else
                    {
                        obj = new
                        {
                            Success = false,
                            Message = "Could not delete.. " + country.CountryName + " is used in address list ",
                            operationType = "DELETE"
                        };
                    }

                }
                else
                {
                    obj = new
                    {
                        Success = false,
                        Message = "Country not found!",
                        operationType = "DELETE"
                    };
                    
                }

            }
            catch (Exception ex)
            {                
                obj = new
                {
                    Success = false,
                    Message = ex.Message,
                    operationType = "DELETE"
                };
                    
            }

            return Json(obj, JsonRequestBehavior.DenyGet);
        }

        //------------ city ----------

        [HttpGet]
        public ActionResult CitySave(int countryNo, int cityNo)
        {
            City city = new City();
            city.CityNo = 0;
            city.CountryNo = 0;
            city.CityName = "";


            if (cityNo > 0)
            {
                city = Repository.GetCityList().Where(c => c.CityNo == cityNo).FirstOrDefault();
            }

            return PartialView(city);

        }





        [HttpPost]
        public JsonResult CitySave(City c)
        {
            object obj = null;

            if (ModelState.IsValid)
            {
                if (c.CityNo == 0)
                {
                    //find last record
                    //if it is database system no need to this line. Probably the CityNo would be autoincrement
                    c.CityNo = Data.Repository.GetCityList().OrderBy(x => x.CityNo).Last().CityNo + 1;
                    Data.Repository.GetCityList().Add(c);

                    obj = new
                    {
                        Success = true,
                        Message = "City Saved successfully",
                        Object = c,
                        operationType = "INSERT",
                        Html = this.RenderPartialView("_cityLine", c)
                    };
                }
                else
                {
                    City city = Repository.GetCityList().Where(x => x.CityNo == c.CityNo).FirstOrDefault();
                    city.CityName = c.CityName;

                    obj = new
                    {
                        Success = true,
                        Message = "City updated successfully",
                        Object = city,
                        operationType = "UPDATE",
                        Html = this.RenderPartialView("_cityLine", city)
                    };
                }

            }
            else
            {
                obj = new { Success = false, Message = "Please check form" };
            }

            return Json(obj, JsonRequestBehavior.DenyGet);
        }



        [HttpPost]
        public JsonResult DeleteCity(int cityNo)
        {
            object obj = null;

            try
            {
                City city = Data.Repository.GetCityList().Where(c => c.CityNo == cityNo).FirstOrDefault();

                if (city != null)
                {

                    int isCityUsed = Data.Repository.GetAddressList().Where(x => x.CityNo == city.CityNo).Count();

                    if (isCityUsed == 0)
                    {
                        Data.Repository.GetCityList().Remove(city);


                        obj = new
                        {
                            Success = true,
                            Message = "City Deleted",
                            operationType = "DELETE"
                        };
                    }
                    else
                    {
                        obj = new
                        {
                            Success = false,
                            Message = "Could not delete.. " + city.CityName + " is used in address list ",
                            operationType = "DELETE"
                        };
                    }

                }
                else
                {
                    obj = new
                    {
                        Success = false,
                        Message = "City not found!",
                        operationType = "DELETE"
                    };

                }

            }
            catch (Exception ex)
            {
                obj = new
                {
                    Success = false,
                    Message = ex.Message,
                    operationType = "DELETE"
                };

            }

            return Json(obj, JsonRequestBehavior.DenyGet);
        }


        //------------ /city ----------


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) NEBULACT
United Kingdom United Kingdom
Necmettin Demir is developer at NEBULACT Ltd. in London/UK.
He has BSc and MSc degrees of Computer Science. He was also graduated from MBA.
He is also trying to share his technical experience writing articles.

Comments and Discussions