Tuesday 13 January 2015

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

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

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.

10 comments:

  1. 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
  2. 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
  3. Thank you for taking the time to provide us with your valuable information.
    Freshers Jobs in Chennai

    ReplyDelete
  4. Thank you for taking the time to provide us with your valuable information.
    Freshers Jobs in Chennai

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. It is very good and useful for students and developer .Learned a lot of new things from your post!Good creation ,thanks for good info .Net Online Training Bangalore

    ReplyDelete

© 2012-2018 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.