Web
Analytics
ASP.NET MVC CRUD USING WEB API 2 and ENTITY Framework | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Step 1: First create info table in database.

Step 2: Add ADO.NET ENTITY DATA Model.

 

Step 3. Create Web API controller with read write scaffolding option.

Step 4: Now run and see output of API Controller(Employee)

Step 5: Add a action in previously created home controller and attach a view.

 

INFO TABLE:

 

CREATE TABLE [dbo].[info](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
	[fname] [varchar](50) NULL,
 CONSTRAINT [PK_info] 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

SET ANSI_PADDING OFF
GO

WEBAPI Conntroller




using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WEBAPIVideoDEMO.Models;

namespace WEBAPIVideoDEMO.Controllers
{
    public class infoesController : ApiController
    {
        private Ganesha3Entities db = new Ganesha3Entities();

        // GET: api/infoes
        public IQueryable<info> Getinfoes()
        {
            return db.infoes;
        }

        // GET: api/infoes/5
        [ResponseType(typeof(info))]
        public IHttpActionResult Getinfo(int id)
        {
            info info = db.infoes.Find(id);
            if (info == null)
            {
                return NotFound();
            }

            return Ok(info);
        }

        // PUT: api/infoes/5
        [ResponseType(typeof(void))]
        public IHttpActionResult Putinfo(int id, info info)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != info.id)
            {
                return BadRequest();
            }

            db.Entry(info).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!infoExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/infoes
        [ResponseType(typeof(info))]
        public IHttpActionResult Postinfo(info info)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.infoes.Add(info);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = info.id }, info);
        }

        // DELETE: api/infoes/5
        [ResponseType(typeof(info))]
        public IHttpActionResult Deleteinfo(int id)
        {
            info info = db.infoes.Find(id);
            if (info == null)
            {
                return NotFound();
            }

            db.infoes.Remove(info);
            db.SaveChanges();

            return Ok(info);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool infoExists(int id)
        {
            return db.infoes.Count(e => e.id == id) > 0;
        }
    }
}

Home Controller

 

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

namespace WEBAPIVideoDEMO.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

Index View(Index.cshtml)

 




 

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>

<table id="tab1" width="300px" border="1">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>FatherName</th>
    </tr>

</table>
ID: <input type="text" name="id" id="id" value="" /><br />
Name : <input type="text" name="name" id="name" value="" /><br />

Father Name: <input type="text" name="fname" id="fname" value="" /><br />
<input type="button" name="btn" id="btn1" value="Add Employee " />
<input type="button" name="btn2" id="btn2" value="Delete Employee " />

<input type="button" name="btn3" id="btn3" value="Update Employee " />

<script>
    $(document).ready(function () {

        $("#tab1").css("display", "none");
        $.getJSON("/api/infoes/", function (data) {
            $("#tab1").css("display", "block");
            $.each(data, function (key, val) {
                $("<tr><td>" + val.id + "</td><td>" + val.name + "</td><td>" + val.fname + "</td></tr>").appendTo($("#tab1"));


            })
        })
        //POST
        $("#btn1").click(function () {
            var name = $("#name").val();
            var fname = $("#fname").val();
            $.ajax({

                type: "POST",

                contentType: "application/json;charset=utf-8",

                url: "/api/infoes/",

                data: JSON.stringify({



                    name: $("#name").val(),

                    fname: $("#fname").val(),



                }),

                dataType: "JSON",

                success: function (data) {
                    $("#tab1").find("tr:gt(0)").remove();
                    $.getJSON("/api/infoes/", function (data) {
                        $("#tab1").css("display", "block");
                        $.each(data, function (key, val) {
                            $("<tr><td>" + val.id + "</td><td>" + val.name + "</td><td>" + val.fname + "</td></tr>").appendTo($("#tab1"));


                        })

                    })



                }
            });


        })
        //Delete
        $("#btn2").click(function () {
            var id = $("#id").val();

            $.ajax({

                type: "DELETE",

                contentType: "application/json;charset=utf-8",

                url: "/api/infoes/" + $("#id").val(),

                data: JSON.stringify({



                    name: $("#id").val(),





                }),

                dataType: "JSON",

                success: function (data) {
                    $("#tab1").find("tr:gt(0)").remove();
                    $.getJSON("/api/infoes/", function (data) {
                        $("#tab1").css("display", "block");
                        $.each(data, function (key, val) {
                            $("<tr><td>" + val.id + "</td><td>" + val.name + "</td><td>" + val.fname + "</td></tr>").appendTo($("#tab1"));


                        })

                    })



                }
            });


        })

        //Update 
        $("#btn3").click(function () {
            var id = $("#id").val();
            var name = $("#name").val();
            var fname = $("#fname").val();
            $.ajax({

                type: "put",

                contentType: "application/json;charset=utf-8",

                url: "/api/infoes/" + $("#id").val(),

                data: JSON.stringify({


                    id: $("#id").val(),
                    name: $("#name").val(),


                    fname: $("#fname").val(),



                }),

                dataType: "JSON",

                success: function (data) {
                    $("#tab1").find("tr:gt(0)").remove();
                    $.getJSON("/api/infoes/", function (data) {
                        $("#tab1").css("display", "block");
                        $.each(data, function (key, val) {
                            $("<tr><td>" + val.id + "</td><td>" + val.name + "</td><td>" + val.fname + "</td></tr>").appendTo($("#tab1"));


                        })

                    })



                }
            });


        })
    });

</script>

 

asp.net core 2.0 with angular online training