Follow Us

LightBlog

Breaking

Saturday, July 11, 2020

ASP.Net MVC Full CRUD operation Using Entity Framework DB First.


Step 1: Create DB and table Name.

Database Name: DB_Crud_test 
Table Name : tbl_student
Step 2: Create web MVC Project

Step 3  Make folder and add ADO.Net Entity Data Model.

       Context  is folder name Where ADO.net Entity model are store.

In the server Name Prese .(DOT) then Database name automatically in the dropdwonlist

Add caption

Step 4 Create a new Empty Controller.

Create a Empty controller name  Student 

  1. Insert Code 

View  Design Code :

@model CRUDE.Context.tbl_student

@{

    ViewBag.Title = "Student";

}

<h2>Student</h2>

<style>

    .error{

        color:red;

    }

</style>

@using (Html.BeginForm("AddStudent", "Student", FormMethod.Post)) // that is rezor syntax

{

<div class="container">

    <div class="form-group">

        <label>Name</label>

        @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })

        @Html.ValidationMessageFor(x => x.Name,"", new {@class="error" })

    </div>

    <div class="form-group">

        <label>Father Name</label>

        @Html.ValidationMessageFor(x => x.Fname, "", new { @class = "error" })

        @Html.TextBoxFor(x => x.Fname, new { @class = "form-control" })

    </div>

    <div class="form-group">

        <label>Email</label>

        @Html.ValidationMessageFor(x => x.Email, "", new { @class = "error" })

        @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })

    </div>

    <div class="form-group">

        <label>Mobile</label>

        @Html.ValidationMessageFor(x => x.Mobile, "", new { @class = "error" })

        @Html.TextBoxFor(x => x.Mobile, new { @class = "form-control" })

    </div>

    <div class="form-group">

        <label>Description</label>

        @Html.ValidationMessageFor(x => x.Description, "", new { @class = "error" })

        @Html.TextAreaFor(x => x.Description, new { @class = "form-control", @rows = "10" })

    </div>

    <div class="form-group">

       <button type="submit" class=" btn-primary">Submit</button>

    </div>

</div>

}


Controller Code Add Student record in the database 

using CRUDE.Context;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace CRUDE.Controllers

{

    public class StudentController : Controller

    {

        DB_Crud_testEntities dbobj = new DB_Crud_testEntities();

        public ActionResult Student()

        {

            return View();

        }

        // mvcaction4 prese Tab

        [HttpPost]

        public ActionResult AddStudent(tbl_student model)

        {

            if (ModelState.IsValid)

            {

                tbl_student obj = new tbl_student();

                obj.Name = model.Name;

                obj.Fname = model.Fname;

                obj.Email = model.Email;

                obj.Mobile = model.Mobile;

                obj.Description = model.Description;

                dbobj.tbl_student.Add(obj);

                dbobj.SaveChanges();

            }

            ModelState.Clear();


            return View("Student");

        }

    }

}

Model : 

//------------------------------------------------------------------------------

// <auto-generated>

//     This code was generated from a template.

//

//     Manual changes to this file may cause unexpected behavior in your application.

//     Manual changes to this file will be overwritten if the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------


namespace CRUDE.Context

{

    using System;

    using System.Collections.Generic;

    using System.ComponentModel.DataAnnotations;


    public partial class tbl_student

    {

        public int ID { get; set; }

        [Required(ErrorMessage ="Please select Name")]

        public string Name { get; set; }

        [Required(ErrorMessage = "Please select father Name")]

        public string Fname { get; set; }

        [Required(ErrorMessage = "Please select email Name")]

        [EmailAddress]

        public string Email { get; set; }

        [Required(ErrorMessage = "Please select Mobile Number")]

        [MinLength(11,ErrorMessage ="Mobile Number should be 11 digit")]

        public string Mobile { get; set; }

        [Required(ErrorMessage = "Please select Description")]

        public string Description { get; set; }

    }

}

Student  Data show in the table.

Step 1  fist create a Action method  Studentlist.
      public ActionResult Studentlist()
        {
            return View();
        }
then create a view.
Student data show code 

public ActionResult Studentlist()
        {
            var res = dbobj.tbl_student.ToList();
            return View(res);
        }
Student  data show in table form code.

@model List<CRUDE.Context.tbl_student>
@{
    ViewBag.Title = "Studentlist";
}

<h2>Studentlist</h2>

<table class="table table-hover">
    <tr>
        <th>Name</th>
        <th>FName</th>
        <th>Email</th>
        <th>Mobile</th>
        <th>Description</th>
        <th>Action</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Name</td>
            <td>@item.Fname</td>
            <td>@item.Email</td>
            <td>@item.Mobile</td>
            <td>@item.Description</td>
            <td><a href="#"><i class="glyphicon-pencil"></i></a></td>
            <td><a href="#"><i class="glyphicon-trash"></i></a></td>
        </tr>
    }

</table>

  Student  data Delete code int  the database
<td><a href="@Url.Action("Delete",new {item.ID })" class="btn btn-danger">Delete</a></td>

in the view  here Delete is Action method.

public ActionResult Delete(int id)
        {
            var res = dbobj.tbl_student.Where(x => x.ID == id).First();
            dbobj.tbl_student.Remove(res);
            dbobj.SaveChanges();
            var list = dbobj.tbl_student.ToList();
            return View("Studentlist",list); // here Studentlist is view name and list is all data remaining in data
        }

Edit Data code 
   public ActionResult Student(tbl_student obj)
        {
            if (obj != null)
                return View(obj);
            else
            return View();
        }

  
Here ID is hiden
@Html.HiddenFor(x=>x.ID)

Update  code :
 public ActionResult AddStudent(tbl_student model)
        {
            if (ModelState.IsValid)
            {
                if (model.ID==0)//insert Code
                {
                    tbl_student obj = new tbl_student();
                    obj.Name = model.Name;
                    obj.Fname = model.Fname;
                    obj.Email = model.Email;
                    obj.Mobile = model.Mobile;
                    obj.Description = model.Description;
                    dbobj.tbl_student.Add(obj);
                    dbobj.SaveChanges();
                }
                else
                {
                    // UPDATE code in single Action Method

                    tbl_student obj = new tbl_student();
                    obj.ID = model.ID;
                    obj.Name = model.Name;
                    obj.Fname = model.Fname;
                    obj.Email = model.Email;
                    obj.Mobile = model.Mobile;
                    obj.Description = model.Description;
                    dbobj.Entry(obj).State = EntityState.Modified;
                    dbobj.SaveChanges();
                }
                
            }
            ModelState.Clear();

            return View("Student");
        }



No comments:

Post a Comment