CRUD Operation Entity Framework using asp.net MVC

=> Add Ado.Net Entity Data model template

=> Choose data base required data

=> Add Add controller

=> HomeController.cs
..............................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Data;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        UniversalEntities db = new UniversalEntities();
        public ActionResult Index()
        {
           List<tvl_Country> obj= db.tvl_Country.ToList();
           return View(obj);
        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(tvl_Country obj)
        {
           tvl_Country obj1=db.tvl_Country.Add(obj);
           db.SaveChanges();
           return RedirectToAction("Index");
        }
   
        public ActionResult Details(int Id)
        {
            tvl_Country obj = db.tvl_Country.SingleOrDefault(x => x.CountryId == Id);
            return View(obj);
        }
        public ActionResult Edit(int Id)
        {
            tvl_Country obj = db.tvl_Country.SingleOrDefault(x => x.CountryId == Id);
            return View(obj);
        }
        [HttpPost]
        public ActionResult Edit(tvl_Country objCountry)
        {
            tvl_Country obj = db.tvl_Country.SingleOrDefault(x => x.CountryId == objCountry.CountryId);
            obj.CountryName = objCountry.CountryName;
            obj.ISDCode = objCountry.ISDCode;          
            db.SaveChanges();        
            return RedirectToAction("Index");

        }
         public ActionResult Delete(int Id)
        {
            tvl_Country obj = db.tvl_Country.SingleOrDefault(x => x.CountryId == Id);
            return View(obj);
        }
         [HttpPost]
         public ActionResult Delete(string strId)
         {
             int i = int.Parse(strId);
             tvl_Country obj = db.tvl_Country.SingleOrDefault(x => x.CountryId == i);
             db.tvl_Country.Remove(obj);          
             db.SaveChanges();
             return RedirectToAction("Index");
         }
    }
}



=>
Add views folder for all Operations

Index.cshtml




@model IEnumerable<MvcApplication1.Models.tvl_Country>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CountryId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CountryName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ISDCode)
        </th>
        <th> Actions</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CountryId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountryName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ISDCode)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new {id=item.CountryId}) |
            @Html.ActionLink("Details", "Details", new {id=item.CountryId}) |
            @Html.ActionLink("Delete", "Delete", new {id=item.CountryId})
        </td>
    </tr>
}

</table>






Create.cshtml



@model MvcApplication1.Models.tvl_Country

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>tvl_Country</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CountryId)
            @Html.ValidationMessageFor(model => model.CountryId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CountryName)
            @Html.ValidationMessageFor(model => model.CountryName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ISDCode)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ISDCode)
            @Html.ValidationMessageFor(model => model.ISDCode)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



Delete.cshtml




@model MvcApplication1.Models.tvl_Country

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>tvl_Country</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.CountryId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CountryId)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.CountryName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CountryName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.ISDCode)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.ISDCode)
    </div>
</fieldset>
@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}





Details.cshtml





@model MvcApplication1.Models.tvl_Country

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>tvl_Country</legend>

    <div class="display-label">
        @Html.DisplayNameFor(model => model.CountryId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CountryId)
    </div>

    <div class="display-label">
        @Html.DisplayNameFor(model => model.CountryName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CountryName)
    </div>

    <div class="display-label">
        @Html.DisplayNameFor(model => model.ISDCode)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.ISDCode)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.CountryId }) |
    @Html.ActionLink("Back to List", "Index")
</p>






Edit.cshtml



@model MvcApplication1.Models.tvl_Country

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>tvl_Country</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CountryId)
            @Html.ValidationMessageFor(model => model.CountryId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CountryName)
            @Html.ValidationMessageFor(model => model.CountryName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ISDCode)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ISDCode)
            @Html.ValidationMessageFor(model => model.ISDCode)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Comments

Post a Comment

Popular posts from this blog

Reverse Sentence using c#

How to write Pure java script Program?