Saturday, 21 January 2017

Simple CRUD Operations In ASP.NET MVC 5

Tags

Here I am Solve Simple CRUD Operations In ASP.NET MVC 5

The Below Controller and Views for Retrieve the database Data 

The model is same for all CRUD operation.

Here Used my  Model Name = tb_emp ; Controller Name= AUD Controller; 

Model


namespace NewMVC.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class tb_emp
    {
        [ScaffoldColumn(false)]
        public int id { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public string Mark { get; set; }
    }
}


Controller for List;-

 public ActionResult Index()
        {
            var emp = db.tb_emp.ToList();
            return View(emp);
        }


View For List



@model IEnumerable
 @{
    ViewBag.Title = "Index";
}

Index

@Html.ActionLink("Add New", "Add","AUD", null) @foreach(var emp in Model) { }
ID Name Age Mark Edit Delete
@emp.id @emp.Name @emp.Age @emp.Mark @Html.ActionLink("Edit", "Edit", "AUD", new { id= @emp.id}, null) @Html.ActionLink("Delete", "Delete", "AUD", new { id = @emp.id }, null)
Output;-



Add Controller:-

  [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(tb_emp emp)
        {
            db.tb_emp.Add(emp);
            db.SaveChanges();
            return RedirectToAction("Index");
        } 

Add View:-
@model NewMVC.Models.tb_emp
@{
    ViewBag.Title = "Add";
}
<h2>Add</h2>
<!DOCTYPE html>

<html>
<head>
    <title>Add</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            @Html.EditorForModel("tb.emp")
            <br />
            <input type="submit" value="Submit" />       
        }
    </div>
</body>
</html>

Add Output:-




Edit Controller:-
 public ActionResult Edit(int id)
        {
            var emp = db.tb_emp.Where(c => c.id == id).FirstOrDefault();
            return View(emp);
        }

        [HttpPost]
        public ActionResult Edit(tb_emp emp)
        {

            db.Entry<tb_emp>(emp).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");

        }

Edit View;-

@model NewMVC.Models.tb_emp
@{
    ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<html>
<head>
    <title>Edit</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            @Html.EditorForModel("tb.emp")
            <br />
            <input type="submit" value="Submit" />
        }
    </div>
</body>
</html>

Edit Output;-

Delete Controller:-

  public ActionResult Delete(int id)
        {
            var emp = db.tb_emp.Where(m => m.id == id).FirstOrDefault();
            db.tb_emp.Remove(emp);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

Delete View and Output:-

we are not using separate page for Delete. 

This Is The Newest Post


EmoticonEmoticon