[index.cshtml]
<ejs-grid id="Grid" allowPaging="true" allowSorting="true" allowFiltering="true" toolbar="@(new List<string>() { "Add","Delete","Update","Cancel" })">
<e-grid-editSettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Normal"></e-grid-editSettings>
<e-data-manager url="/odata/Books" adaptor="ODataV4Adaptor"></e-data-manager>
<e-grid-columns>
<e-grid-column field="guid" headerText="GuID" isPrimaryKey="true" width="150"></e-grid-column>
. . . .
</e-grid-columns>
</ejs-grid>
[BookController.cs]
// GET api/values
[HttpGet]
[EnableQuery]
[System.Web.Http.AllowAnonymous]
public IQueryable<Book> Get()
{
return _db.Books;
}
// POST api/values
[HttpPost]
[EnableQuery]
[System.Web.Http.AllowAnonymous]
public IActionResult Post([FromBody]Book book)
{
_db.Books.Add(book);
_db.SaveChanges();
return Created(book);
}
// PUT api/values/"5"
[HttpPut("{id}")]
[Route("api/[controller]")]
[System.Web.Http.AllowAnonymous]
public async Task<Book> Put(string id, [FromBody] Book book)
{
var entity = await _db.Books.FindAsync(book.guid);
_db.Entry(entity).CurrentValues.SetValues(book);
await _db.SaveChangesAsync();
return book;
}
// DELETE api/values/5
[HttpDelete("{id}")]
[System.Web.Http.AllowAnonymous]
public async Task<int> Delete(int key)
{
var od = await _db.Books.FindAsync(key);
_db.Books.Remove(od);
await _db.SaveChangesAsync();
return key;
}
|
[Book.cs]
public class Book
{
[Key]
// Please ensure the Guid field type declaration
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid guid { get; set; }
public int Id { get; set; }
public string ISBN { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public decimal Price { get; set; }
}
|