0% found this document useful (0 votes)
61 views3 pages

MVC Delete Example With WebAPI Sevice and Entity Framework - New

The document describes the steps to create a Web API service that allows deleting records from an Entity Framework database table. An MVC application is then created as a client to call the DELETE method on the Web API service. The key steps are: 1. Create a Web API project with Entity Framework to access a database table and add a method to delete a record by ID. 2. Create an MVC application and add a controller to call the DELETE method on the Web API service using HTTP requests. 3. Add a view to the MVC application with a form to accept user input for the record ID to delete and display deletion status. 4. Both the Web API service and MVC client projects

Uploaded by

Mahesh Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

MVC Delete Example With WebAPI Sevice and Entity Framework - New

The document describes the steps to create a Web API service that allows deleting records from an Entity Framework database table. An MVC application is then created as a client to call the DELETE method on the Web API service. The key steps are: 1. Create a Web API project with Entity Framework to access a database table and add a method to delete a record by ID. 2. Create an MVC application and add a controller to call the DELETE method on the Web API service using HTTP requests. 3. Add a view to the MVC application with a form to accept user input for the record ID to delete and display deletion status. 4. Both the Web API service and MVC client projects

Uploaded by

Mahesh Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MVC EF Delete using Web API service

Creating WebAPI Service:

Step-1: Create Web API application with .Net Framework 4.5

Step-2: Add Entity Framework Model with Emp table

Step-3: Add Model class file that is Employee.cs and write below code within Employee.cs

public class Employee


{
EdatabaseEntities objdb = new EdatabaseEntities();

public int Eno { get; set; }

public int DeleteEmp()


{
emp objemp = objdb.emps.Find(Eno);
try
{
objdb.emps.Remove(objemp);
objdb.SaveChanges();
return 1;
}
catch
{
return 0;
}
}
}

Step-4: Goto ValuesController.cs and import Model

 Update old Delete method that is void Delete() as

// GET api/values/5
Employee objmodel = new Employee();

public int Delete(int id)


{
objmodel.Eno = id;
return objmodel.DeleteEmp();
}

Step-5: build and run the application

Creating ASP.Net MVC client:

Step-1: Create ASP.Net MVC application with .Net Framework 4.5

Step-2: Add Controller (EmpController.cs)

 Write below within EmpController.cs


 Import System.Net.Http;
MVC EF Delete using Web API service
using System.Net.Http;

namespace MvcWebApiDsiplay.Controllers
{
public class EmpController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult DeleteEmp()
{
int eno = int.Parse(Request["txtEno"]);
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:41117/api/");
var querystring = client.DeleteAsync("values/" + eno);
ViewData["delvalue"] =
querystring.Result.Content.ReadAsAsync<int>().Result;
ForwardEmp();
return View("Index");
}
}
}

Step-3: Adding View(Index.cshtml)

 Wrtite below code with in Index.cshtml

@{
Layout = null;
string msg = "";
if(this.IsPost)
{
if(Request["btnDelete"]=="Delete")
{
int i = (int)ViewData["delvalue"];
if(i==1)
{
msg = "Record is Deleted";
}
else
{
msg = "Record is not Deleted";
}
}
}
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form name="myform" method="post">
<div>
MVC EF Delete using Web API service
Enter Empno <input type="text" name="txtEno" />
<br/><br/>
<input type="submit" name="btnDelete" value="Delete"
formaction="~/Emp/DeleteEmp" />
<br/><br/>
@Html.Label("lblMsg",msg)
</div>
</form>
</body>
</html>

Step-4: Update RouteConfig.cs

Controller=”Emp”

Note: while Debugging MVC application(Client application) Web API Service Application must have
to in Debugging mode until then it will throw error

You might also like