Tuesday, 13 January 2015

Mvc 4 Razor CRUD Operation using C# || ASP.NET MVC 4 application to Create/Insert,Read,Update,Delete and Search functionality using Razor view engine and Entity Framework

Hi friends, in this article I will explain about  MVC 4 Razor CRUD Operation using C# || ASP.NET MVC 4 application to Create/Insert,Read,Update,Delete and Search functionality using Razor view engine and Entity Framework.

Create one STUDENTS table as below.
CREATE TABLE STUDENTS(
ID INT PRIMARY KEY IDENTITY,
FIRSTNAME VARCHAR(50),
LASTNAME VARCHAR(50),
ADDRESS VARCHAR(150)
)

Create new project and select ASP.NET MVC4 Web Application as shown in the below figure.



Right click on root Tag add new Item –click left side Data --ADO.NET Entity Data model  and give name as StudentModel.edmx as shown in the below figure.

Add your connection and give the Entity name (web.config connectionstring) as StudentEntity and Model as STUDENTModel
Press Cntl+Shift+B or Build the solution.
After that add controller as shown in the below figure.

StudentController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcStudentData.Controllers
{
    public class StudentController : Controller
    {
        private StudentEntities db = new StudentEntities();

        //
        // GET: /Student/

        public ActionResult Index()
        {
            return View(db.STUDENTS.ToList());
        }

        //
        // GET: /Student/Details/5

        public ActionResult Details(int id = 0)
        {
            STUDENT student = db.STUDENTS.Single(s => s.ID == id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // GET: /Student/Create

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

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(STUDENT student)
        {
            if (ModelState.IsValid)
            {
                db.STUDENTS.AddObject(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(student);
        }

        //
        // GET: /Student/Edit/5

        public ActionResult Edit(int id = 0)
        {
            STUDENT student = db.STUDENTS.Single(s => s.ID == id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(STUDENT student)
        {
            if (ModelState.IsValid)
            {
                db.STUDENTS.Attach(student);
                db.ObjectStateManager.ChangeObjectState(student, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Student/Delete/5

        public ActionResult Delete(int id = 0)
        {
            STUDENT student = db.STUDENTS.Single(s => s.ID == id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            STUDENT student = db.STUDENTS.Single(s => s.ID == id);
            db.STUDENTS.DeleteObject(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}


Run the application then the output will like as shown in the below figures.

When we click on Create New Link then below page will be open.


When we click on Delete Link then below page will be open.


When we click on Details Link then below page will be open.


When we click on Edit Link then below page will be open.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."

7 comments:

  1. nic post..
    http://mkniit.blogspot.in

    ReplyDelete
  2. using screenshots providing a well explanation which mvc informative are most helpful to know more. It explaining with coding representation successfully.



    Dot Net Training in Chennai

    ReplyDelete
  3. I would be really understand for this concepts of MVC it's most helpful to me.I'll working this code result was really successful..

    Dotnet Training in Chennai

    ReplyDelete


  4. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.