MVC 3 Insert update delete razor

Ravikumar
4

MVC 3  Insert update delete razor :-

Create Table like this:-

Paste Below code:-

USE [Demo]
GOSET ANSI_NULLS ON
GOSET QUOTED_IDENTIFIER ON
GOCREATE TABLE [dbo].[Ravi_wpf_regs](
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [FirstName] [nvarchar](50) NULL,
      [LastName] [nvarchar](50) NULL,
      [Email] [nvarchar](50) NULL,
      [Password] [nvarchar](50) NULL,
      [Address] [nvarchar](50) NULL,
 CONSTRAINT [PK_Ravi_wpf_regs] PRIMARY KEY CLUSTERED(      [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Here Create a new Project From file:-
Now Select Internet Application:-
You See Like this:-

Now Goto Solution explorer and add User.cs(Class File) in models:-



Now type given code in User.cs file:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcIDU.Models
    {
    public class User
        {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Address { get; set; }

        }
    }


add new linq context Class:-




Add table :-
Now add Controller:-







Type Below Code in Usercontroller.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcIDU.Models;

namespace MvcIDU.Controllers
    {
    public class UserController : Controller
        {
        //
        // GET: /User/
        DataClassesDataContext db = new DataClassesDataContext();
        public ActionResult Index()
            {
            var query = from d in db.Ravi_wpf_regs select d;
            var userlist = new List<MvcIDU.Models.User>();
            if (query.Any())
                {
                foreach (var item in query.ToList())
                    {
                    userlist.Add(new MvcIDU.Models.User
                    {
                        ID = item.ID,
                        FirstName = item.FirstName,
                        LastName = item.LastName,
                        Email = item.Email,
                        Password = item.Password,
                        Address = item.Address
                    });
                    }
                }
            return View(userlist);
            }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
            {
            var user = new User();
            var h = db.Ravi_wpf_regs.FirstOrDefault(p => p.ID == id);
            user.ID = h.ID;
            user.FirstName = h.FirstName;
            user.LastName = h.LastName;
            user.Email = h.Email;
            user.Address = h.Address;
            return View(user);
            }

        //
        // GET: /User/Create

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

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
            {
            try
                {
                // TODO: Add insert logic here
                var user = new User();
                var h = new Ravi_wpf_reg();
                h.FirstName = user.FirstName;
                h.LastName = user.LastName;
                h.Email = user.Email;
                h.Address = user.Address;
                db.Ravi_wpf_regs.InsertOnSubmit(h);
                db.SubmitChanges();
                return RedirectToAction("Index");
                }
            catch
                {
                return View();
                }
            }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
            {
            var user = new User();
            var h = db.Ravi_wpf_regs.FirstOrDefault(p => p.ID == id);
            user.FirstName = h.FirstName;
            user.LastName = h.LastName;
            user.Email = h.Email;
            user.Address = h.Address;
            return View(user);
            }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, User user)
            {
            try
                {
                // TODO: Add update logic here
                var h = db.Ravi_wpf_regs.FirstOrDefault(p => p.ID == id);
                h.FirstName = user.FirstName;
                h.LastName = user.LastName;
                h.Email = user.Email;
                h.Password = user.Password;
                h.Address = user.Address;
                db.SubmitChanges();
                return RedirectToAction("Index");
                }
            catch
                {
                return View();
                }
            }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
            {
            var user = new User();
            var h = db.Ravi_wpf_regs.FirstOrDefault(p => p.ID == id);
            user.FirstName = h.FirstName;
            user.LastName = h.LastName;
            user.Email = h.Email;
            user.Password = h.Password;
            user.Address = h.Address;
            return View(user);
            }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, User user)
            {
            try
                {
                // TODO: Add delete logic here
                var h = db.Ravi_wpf_regs.FirstOrDefault(p => p.ID == id);
                h.FirstName = user.FirstName;
                h.LastName = user.LastName;
                h.Email = user.Email;
                h.Password = user.Password;
                h.Address = user.Address;
                db.Ravi_wpf_regs.DeleteOnSubmit(h);
                db.SubmitChanges();
                return RedirectToAction("Index");
                }
            catch
                {
                return View();
                }
            }
        }
    }



add view For insert ,Details,Edit,Delete Select list:-






u will see code like this:-


@model IEnumerable<MvcIDU.Models.User>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th>
            Email
        </th>
        <th>
            Password
        </th>
        <th>
            Address
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>









Now change in Global.asax.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcIDU
    {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
        {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
            filters.Add(new HandleErrorAttribute());
            }

        public static void RegisterRoutes(RouteCollection routes)
            {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "User", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            }

        protected void Application_Start()
            {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            }
        }
    }


Now Run this project:-
Index page:


Edit(Update) :-



Details:



Delete:



Delete :



I hope This Program will help You .If You have any Query Then type.I will try to solve Your Query.

-Ravikumhar





























Tags:

Post a Comment

4Comments

  1. yes your code is very help full but it not properly

    ReplyDelete
  2. thank you Bhavesh Vyas ...if there is problem in code then you can ask me...

    ReplyDelete
  3. creating store procedure first is compulsory to run this code

    ReplyDelete
  4. sir i got following error in index.cshtml file
    Object reference not set to an instance

    ReplyDelete
Post a Comment