ODATA CODE:
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.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using Test.Models;
namespace Test.Controllers
{
/*
To add a route for this controller, merge these statements into the Register method of the WebApiConfig class. Note that OData URLs are case sensitive.
using System.Web.Http.OData.Builder;
using Test.Models;
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<EMP1>("Emp");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
*/
public class EmpController : ODataController
{
private TestEntities db = new TestEntities();
// GET odata/Emp
[Queryable]
public IQueryable<EMP1> GetEmp()
{
return db.EMP1;
}
// GET odata/Emp(5)
[Queryable]
public SingleResult<EMP1> GetEMP1([FromODataUri] int key)
{
return SingleResult.Create(db.EMP1.Where(emp1 => emp1.EID == key));
}
// PUT odata/Emp(5)
public IHttpActionResult Put([FromODataUri] int key, EMP1 emp1)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != emp1.EID)
{
return BadRequest();
}
db.Entry(emp1).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EMP1Exists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(emp1);
}
// POST odata/Emp
public IHttpActionResult Post(EMP1 emp1)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.EMP1.Add(emp1);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (EMP1Exists(emp1.EID))
{
return Conflict();
}
else
{
throw;
}
}
return Created(emp1);
}
// PATCH odata/Emp(5)
[AcceptVerbs("PATCH", "MERGE")]
public IHttpActionResult Patch([FromODataUri] int key, Delta<EMP1> patch)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
EMP1 emp1 = db.EMP1.Find(key);
if (emp1 == null)
{
return NotFound();
}
patch.Patch(emp1);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EMP1Exists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(emp1);
}
// DELETE odata/Emp(5)
public IHttpActionResult Delete([FromODataUri] int key)
{
EMP1 emp1 = db.EMP1.Find(key);
if (emp1 == null)
{
return NotFound();
}
db.EMP1.Remove(emp1);
db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EMP1Exists(int key)
{
return db.EMP1.Count(e => e.EID == key) > 0;
}
}
}
@{
ViewBag.Title = "Index";
}
<table cellpadding="0" cellspacing="0" align="center" style="left:80px; top:20px; position:relative">
<tr><td>EID</td><td>:</td><td><input type="text" id="tb1" /></td></tr>
<tr><td>NAME</td><td>:</td><td><input type="text" id="tb2" /></td></tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="button" id="btn" value="Save" style="width:80px" /> <input type="button" id="btn1" value="Update" style="width:80px" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<table id="tb" border="1" cellpadding="0" cellspacing="0"></table>
</td>
</tr>
</table>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
function x() {
$.ajax({
url: '/odata/Emp',
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: 'json',
success: function (data) {
$('#tb').empty().append("<tr><th>UPDATE</th><th>DELETE</th><th>EID</th><th>NAME</th></tr>");
for (var i = 0; i < data.value.length; i++)
$('#tb').append("<tr><td><a href='#' onclick=z('" + data.value[i].EID + "')>Edit</a></td><td><a href='#' onclick=y('" + data.value[i].EID + "')>Delete</a></td><th>" + data.value[i].EID + "</th><th>" + data.value[i].NAME + "</th></tr>");
}
});
}
$(
function () {
x();
$('#btn1').click(function () {
$.ajax({
url: '/odata/Emp('+m+')',
contentType: "application/json; charset=utf-8",
type: 'PUT',
dataType: 'json',
data: "{EID:'" + $('#tb1').val() + "',NAME:'" + $('#tb2').val() + "'}",
success: function (data) {
alert('Data Updated.')
x();
}
});
});
$('#btn').click(function () {
$.ajax({
url: '/odata/Emp',
contentType: "application/json; charset=utf-8",
type: 'POST',
dataType: 'json',
data: "{EID:'" + $('#tb1').val() + "',NAME:'" + $('#tb2').val() + "'}",
success: function (data)
{
alert('Data Saved.')
x();
}
});
});
}
);
var m;
function y(s)
{
if (confirm('Do you want to delete it ?'))
{
$.ajax({
url: '/odata/Emp(' + s + ')',
contentType: "application/json; charset=utf-8",
type: 'DELETE',
dataType: 'json',
success: function () {
alert('Data Deleted.')
x();
}
});
}
}
function z(s) {
m = s;
$.ajax({
url: '/odata/Emp('+s+')',
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: 'json',
success: function (data) {
$('#tb1').val(data.EID);
$('#tb2').val(data.NAME);
}
});
}
</script>
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.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using Test.Models;
namespace Test.Controllers
{
/*
To add a route for this controller, merge these statements into the Register method of the WebApiConfig class. Note that OData URLs are case sensitive.
using System.Web.Http.OData.Builder;
using Test.Models;
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<EMP1>("Emp");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
*/
public class EmpController : ODataController
{
private TestEntities db = new TestEntities();
// GET odata/Emp
[Queryable]
public IQueryable<EMP1> GetEmp()
{
return db.EMP1;
}
// GET odata/Emp(5)
[Queryable]
public SingleResult<EMP1> GetEMP1([FromODataUri] int key)
{
return SingleResult.Create(db.EMP1.Where(emp1 => emp1.EID == key));
}
// PUT odata/Emp(5)
public IHttpActionResult Put([FromODataUri] int key, EMP1 emp1)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != emp1.EID)
{
return BadRequest();
}
db.Entry(emp1).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EMP1Exists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(emp1);
}
// POST odata/Emp
public IHttpActionResult Post(EMP1 emp1)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.EMP1.Add(emp1);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (EMP1Exists(emp1.EID))
{
return Conflict();
}
else
{
throw;
}
}
return Created(emp1);
}
// PATCH odata/Emp(5)
[AcceptVerbs("PATCH", "MERGE")]
public IHttpActionResult Patch([FromODataUri] int key, Delta<EMP1> patch)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
EMP1 emp1 = db.EMP1.Find(key);
if (emp1 == null)
{
return NotFound();
}
patch.Patch(emp1);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EMP1Exists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(emp1);
}
// DELETE odata/Emp(5)
public IHttpActionResult Delete([FromODataUri] int key)
{
EMP1 emp1 = db.EMP1.Find(key);
if (emp1 == null)
{
return NotFound();
}
db.EMP1.Remove(emp1);
db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EMP1Exists(int key)
{
return db.EMP1.Count(e => e.EID == key) > 0;
}
}
}
WEBAPI CONFIC CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using Test.Models;
namespace Test
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
//builder.EntitySet<EMP1>("Emp");
//config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
//config.MapHttpAttributeRoutes();
builder.EntitySet<EMP1>("Emp");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
INDEX PAGE CODE:
@{
ViewBag.Title = "Index";
}
<table cellpadding="0" cellspacing="0" align="center" style="left:80px; top:20px; position:relative">
<tr><td>EID</td><td>:</td><td><input type="text" id="tb1" /></td></tr>
<tr><td>NAME</td><td>:</td><td><input type="text" id="tb2" /></td></tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="button" id="btn" value="Save" style="width:80px" /> <input type="button" id="btn1" value="Update" style="width:80px" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<table id="tb" border="1" cellpadding="0" cellspacing="0"></table>
</td>
</tr>
</table>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
function x() {
$.ajax({
url: '/odata/Emp',
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: 'json',
success: function (data) {
$('#tb').empty().append("<tr><th>UPDATE</th><th>DELETE</th><th>EID</th><th>NAME</th></tr>");
for (var i = 0; i < data.value.length; i++)
$('#tb').append("<tr><td><a href='#' onclick=z('" + data.value[i].EID + "')>Edit</a></td><td><a href='#' onclick=y('" + data.value[i].EID + "')>Delete</a></td><th>" + data.value[i].EID + "</th><th>" + data.value[i].NAME + "</th></tr>");
}
});
}
$(
function () {
x();
$('#btn1').click(function () {
$.ajax({
url: '/odata/Emp('+m+')',
contentType: "application/json; charset=utf-8",
type: 'PUT',
dataType: 'json',
data: "{EID:'" + $('#tb1').val() + "',NAME:'" + $('#tb2').val() + "'}",
success: function (data) {
alert('Data Updated.')
x();
}
});
});
$('#btn').click(function () {
$.ajax({
url: '/odata/Emp',
contentType: "application/json; charset=utf-8",
type: 'POST',
dataType: 'json',
data: "{EID:'" + $('#tb1').val() + "',NAME:'" + $('#tb2').val() + "'}",
success: function (data)
{
alert('Data Saved.')
x();
}
});
});
}
);
var m;
function y(s)
{
if (confirm('Do you want to delete it ?'))
{
$.ajax({
url: '/odata/Emp(' + s + ')',
contentType: "application/json; charset=utf-8",
type: 'DELETE',
dataType: 'json',
success: function () {
alert('Data Deleted.')
x();
}
});
}
}
function z(s) {
m = s;
$.ajax({
url: '/odata/Emp('+s+')',
contentType: "application/json; charset=utf-8",
type: 'GET',
dataType: 'json',
success: function (data) {
$('#tb1').val(data.EID);
$('#tb2').val(data.NAME);
}
});
}
</script>
No comments:
Post a Comment