Asp.net MVC Insert, update, delete and Read from Database table Example Code. All CRUD operations.


What is Asp.net MVC?
It is a framework which can be used for developing web applications on .net framework.
MVC stands for Model, View and Controller.

Just follow the below code and do the changes as per your requirement.

PersonController.cs
using MVCCRUD.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCCRUD.Controllers
{
    public class PersonController : Controller
    {
        AJAYTUTORIALSEntities Contextobj = new AJAYTUTORIALSEntities();
        public ActionResult AddPerson()
        {
            PersonModel obj = new PersonModel();
            return View("AddPerson",obj);
        }
        [HttpPost]
        public ActionResult AddPerson(PersonModel obj)
        {
            Contextobj.People.Add(new Person() { PersonName = obj.PersonName, PersonAddress = obj.PersonAddress });
            Contextobj.SaveChanges();

            return View("AddPerson", obj);
        }
        public ActionResult DisplayPersons()
        {
            var PersonRecords = Contextobj.People.ToList();
            return View("DisplayPersons",PersonRecords);
        }
        public ActionResult EditPerson(int personid)
        {
            var personRec = (from item in Contextobj.People
                             where item.PersonId == personid
                             select item).First();
            return View("EditPerson", personRec);
        }
        [HttpPost]
        public ActionResult EditPerson(Person obj)
        {
            var personRec = (from item in Contextobj.People
                             where item.PersonId == obj.PersonId
                             select item).First();

            personRec.PersonName = obj.PersonName;
            personRec.PersonAddress = obj.PersonAddress;

            Contextobj.SaveChanges();

            var PersonRecords = Contextobj.People.ToList();
            return View("DisplayPersons", PersonRecords);
        }
        public ActionResult Deleteperson(int personId)
        {

            var personRec = (from item in Contextobj.People
                             where item.PersonId == personId
                             select item).First();

            Contextobj.People.Remove(personRec);
            Contextobj.SaveChanges();
            var PersonRecords = Contextobj.People.ToList();
            return View("DisplayPersons", PersonRecords);
        }
    }

}

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

namespace MVCCRUD.Models
{
    public class PersonModel
    {
        public string PersonName { get; set; }
        public string PersonAddress { get; set; }
    }
}

AddPerson.cshtml

@{
    ViewBag.Title = "Add Person";
}
@model MVCCRUD.Models.PersonModel


<h2>Add Person</h2>


<form action="AddPerson/Person" method="post">

    Person Name: @Html.TextBoxFor(model => model.PersonName)
    Person Address: @Html.TextBoxFor(model => model.PersonAddress)

    @*<input type="text" name="PersonName" id="PersonName" />*@

    <input type="submit" value="Add Person" />
</form>


DisplayPersons.cshtml

@{
    ViewBag.Title = "DisplayPersons";
}
@model List<Person>
<h2>Display Persons</h2>

<table border="1">

    <tr>
        <th>PersonId</th>
        <th>Person Name</th>
        <th>Person Address</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.PersonId</td>
            <td>@item.PersonName</td>
            <td>@item.PersonAddress</td>
            <td>@Html.ActionLink("Edit","Editperson","Person",new { personid =item.PersonId},new { })</td>
            <td>@Html.ActionLink("Delete", "Deleteperson", "Person", new { personid = item.PersonId }, new { })</td>
        </tr>
    }


</table>


EditPerson.cshtml

@{
    ViewBag.Title = "EditPerson";
}
@model Person
<h2>Edit Person</h2>

<form action="EditPerson/Person" method="post">

    Person Name: @Html.TextBoxFor(model => model.PersonName)
    Person Address: @Html.TextBoxFor(model => model.PersonAddress)
    @Html.HiddenFor(model=>model.PersonId)
    @*<input type="text" name="PersonName" id="PersonName" />*@

    <input type="submit" value="Update Person" />
</form>



2 comments:

Unknown said...

Simple and to the point all code is very simple and easy to understand nice example of asp.net mvc

Unknown said...

thank you so much . can you provide me search code .like already you given .thanks

Post a Comment

Popular Posts