Click here to Skip to main content
15,896,912 members
Articles / Web Development / HTML

A Simple Tutorial on Developing ASP.NET Applications in MVC Pattern

Rate me:
Please Sign up or sign in to vote.
4.82/5 (45 votes)
13 May 2010CPOL17 min read 407.1K   18.1K   142  
This article gives a simple tutorial on developing ASP.NET applications in MVC pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPNetMVCTutorial.Models;

namespace ASPNetMVCTutorial.Controllers
{
    public class StudentsController : Controller
    {
        private StudentsModel GetStudentModelFromSession()
        {
            StudentsModel theModel = (StudentsModel)Session["Students"];
            if (theModel == null)
            {
                theModel = new StudentsModel();
                Session["Students"] = theModel;
            }

            return theModel;
        }

        public ActionResult StudentList()
        {
            StudentsModel theModel = GetStudentModelFromSession();

            ViewData["Students"] = theModel.GetStudents();
            return View();
        }

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

        public ActionResult AddStudentAction()
        {
            string Name = Request.Form["textName"];
            string Score = Request.Form["txtScore"];

            if ((Name == null) || (Name.Trim() == ""))
            {
                ViewData["ERROR"] = "Please provide a name for the student to add";
                return View("../Shared/Error");
            }

            if (Name.Length < 6)
            {
                ViewData["ERROR"]
                    = "The student's name should not be less than 6 characters.";
                return View("../Shared/Error");
            }


            int intScore;
            if (!Int32.TryParse(Score, out intScore))
            {
                ViewData["ERROR"]
                    = "Please provide a valid score to the student to add.";
                return View("../Shared/Error");
            }


            if ((intScore < 60) || (intScore > 100))
            {
                ViewData["ERROR"]
                    = "We only accept students with scores between 60 and 100.";
                return View("../Shared/Error");
            }

            StudentsModel theModel = GetStudentModelFromSession();
            theModel.AddStudent(Name, intScore);

            ViewData["Students"] = theModel.GetStudents();
            return View("StudentList");
        }

        public ActionResult DeleteStudent()
        {
            string ID = Request.QueryString["ID"];
            int intID;

            if (!Int32.TryParse(ID, out intID))
            {
                ViewData["ERROR"] = "Please provide a valid student ID";
                return View("../Shared/Error");
            }

            StudentsModel theModel = GetStudentModelFromSession();
            theModel.DeleteStudent(intID);

            return RedirectToAction("StudentList");
        }
    }
}

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
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

Comments and Discussions