Click here to Skip to main content
15,885,216 members
Articles / Web Development / CSS

A Simple ASP.NET MVC Grid with Multilingual Support+Drop Down Menu

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Nov 2013CPOL 11.2K   4  
Using grid and search grid using Ajax or without Ajax, and using Resource Files to enable multi language support
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class CustomerController : BaseController
    {
        private PagingModel GetCustomers(string term)
        {
            var list = CustomerModel.GetAll();
            if (!string.IsNullOrEmpty(term)) list = list.Where(c => c.LastName.Contains(term) || c.Name.Contains(term)).ToList();
            var paging = new PagingModel();
            paging.ResultList = list;
            
            return paging;
        }

        public JsonResult GetCustomersJson(string term)
        {
            var list = GetCustomers(term).ResultList;
            return Json(list.Select(c => new { label = c.Name }).Distinct().ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index(PagingModel paging)
        {
            var list = GetCustomers(paging.SearchTerm);
            return View(list);
        }

        public ActionResult Grid(string term)
        {
            return PartialView(GetCustomers(term).ResultList);
        }
        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Customer/Create
        public ActionResult Create()
        {
            if (Request.IsAjaxRequest()) return PartialView();
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: Add insert logic here
                CustomerModel.Add(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
            var model = CustomerModel.GetAll().Where(c => c.Id == id).FirstOrDefault();
            return View(model);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: Add insert logic here
                CustomerModel.Edit(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

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) DPI
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions