Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

ASP.NET MVC 4 - Part [1] - Introduction

, 20 Mar 2013 CPOL
Get started with ASP.NET MVC 4, Compare ASP.NET MVC with Webforms, Sample Application, MVC Master Page - Layout, MVC User Control- Partial View, @RenderBody, Membership management, ActionResults
ePhoneBook.zip
ePhoneBook
Controllers
Models
Views
PhoneBook
Shared
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ePhoneBook.Models;
namespace ePhoneBook.Controllers
{
    public class PhoneBookController : Controller
    {
        private static List<PhoneBookModel> _PhoneBookList;

        public static List<PhoneBookModel> PhoneBookList
        {
            get
            {
                if (_PhoneBookList == null)
                {
                    _PhoneBookList = new List<PhoneBookModel>();
                    _PhoneBookList.Add(new PhoneBookModel
                    {
                        FirstName = "Shemeer",
                        LastName = "NS",
                        MobileNumber = "+91 22 123456",
                        Email = "shemeerns@shemeerns.com"
                    });
                }
                return _PhoneBookList;
            }
            set { _PhoneBookList = value; }
        }

        //
        // GET: /PhoneBooks/

        public ActionResult Index()
        {

            return View(PhoneBookList);
        }

        //
        // GET: /PhoneBooks/Details/5

        public ActionResult Details(string mNumber)
        {

            return View(PhoneBookList.Where(c => c.MobileNumber == mNumber).FirstOrDefault());
        }

        //
        // GET: /PhoneBooks/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /PhoneBooks/Create

        [HttpPost]
        public ActionResult Create(PhoneBookModel phModel)
        {
            try
            {
                _PhoneBookList.Add(phModel);
                // TODO: Add insert logic here

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

        //
        // GET: /PhoneBooks/Edit/5

        public ActionResult Edit(string mNumber)
        {
            return View(_PhoneBookList.Where(c => c.MobileNumber == mNumber).FirstOrDefault());
        }

        //
        // POST: /PhoneBooks/Edit/5

        [HttpPost]
        public ActionResult Edit(string mNumber, PhoneBookModel phModel)
        {
            try
            {
                _PhoneBookList.Where(c => c.MobileNumber == mNumber).ToList().ForEach(d => { d=phModel; });
                //d.FirstName = phModel.FirstName; d.MiddleName = phModel.MiddleNamek
                // TODO: Add update logic here

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

        //
        // GET: /PhoneBooks/Delete/5

        public ActionResult Delete(string mNumber)
        {
            try
            {
                _PhoneBookList.RemoveAll(c => c.MobileNumber == mNumber);
                // TODO: Add delete logic here

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

        //
        // POST: /PhoneBooks/Delete/5

        [HttpPost]
        public ActionResult Delete(string mNumber, 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)

Share

About the Author

Shemeer NS
Software Developer (Senior)
India India
Technology Specialist | CodeProject MVP | Visual Studio Gallery Contributor | Author | Geek | Netizen | Husband | ChessPlayer
 
Most of my articles are listed on top 5 of the respective 'Best articles of the month' and some of my articles are published on ASP.NET WebSite's Article of the Day section.
 
Check my contributions in Visual Studio Gallery and Code Project
 
Technical Blog: http://www.shemeerns.com
Facebook: http://facebook.com/shemeernsblog
Twitter : http://twitter.com/shemeerns
Google+ : http://google.com/+Shemeernsblog
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Mar 2013
Article Copyright 2012 by Shemeer NS
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid