MVC Delete Example With WebAPI Sevice and Entity Framework - New
MVC Delete Example With WebAPI Sevice and Entity Framework - New
Step-3: Add Model class file that is Employee.cs and write below code within Employee.cs
// GET api/values/5
Employee objmodel = new Employee();
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");
}
}
}
@{
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>
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