.Net Core 2.1,the cshtml
@using Kendo.Mvc.UI
@(Html.Kendo().Grid<Liwb.Entities.Travel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.ID);
columns.Bound(p => p.Name);
columns.Bound(p => p.Pos);
columns.Bound(p => p.IsDelete);
columns.Bound(p => p.From);
columns.Bound(p => p.To);
columns.Bound(p => p.StartDate).Format("{0:MM/dd/yyyy}");
columns.Bound(p => p.EndDate).Format("{0:MM/dd/yyyy}");
columns.Bound(p => p.IntervalDays);
columns.Bound(p => p.TrafficType);
columns.Bound(p => p.PhotographDirectory);
columns.Bound(p => p.ProblemDocument);
columns.Command(commands =>
{
commands.Edit();
commands.Destroy(); // The "destroy" command removes data items.
}).Title("Commands").Width(200);
})
.Editable(editMode => editMode.Mode(GridEditMode.InLine))
.Pageable()
//.Sortable()
.Scrollable()
//.Filterable()
.HtmlAttributes(new { style = "height:100%; width:100%" })
.DataSource(dataSource => dataSource
.WebApi()
.Model(Model => { Model.Id(p => p.ID);})
.Events(events=>events.Error("error_handler"))
.Read(read => read.Action("Get", "Travel"))
.Create(create => create.Action("Post", "Travel"))
.Update(update => update.Action("Put", "Travel", new { id = "{0}" }))
.Destroy(destory => destory.Action("Delete", "Travel", new { id = "{0}" }))
)
)
the api controller
namespace PlantManagement.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TravelController : ControllerBase
{
private readonly PlantManagementDbContext _context;
public TravelController(PlantManagementDbContext context)
{
_context = context;
}
//MVC return Value: ActionResult
// GET: api/Travel
[HttpGet]
//public IEnumerable<Travel> GetTravels()
public DataSourceResult Get([DataSourceRequest]DataSourceRequest request)
{
return _context.Travels.ToDataSourceResult(request);//.Where(p => p.PID == null)
}
// GET: api/Travel/5
[HttpGet("{id}")]
public async Task<IActionResult> Get([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var travel = await _context.Travels.FindAsync(id);
if (travel == null)
{
return NotFound();
}
return Ok(travel);
}
// PUT: api/Travel/5
[HttpPut("{id}")]
public async Task<IActionResult> Put([FromRoute] long id, [FromBody] Travel travel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != travel.ID)
{
return BadRequest();
}
_context.Entry(travel).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TravelExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Travel
[HttpPost]
public async Task<IActionResult> Post( Travel travel)//[FromBody]
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Travels.Add(travel);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (TravelExists(travel.ID))
{
return new StatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtAction("GetTravel", new { id = travel.ID }, travel);
}
// DELETE: api/Travel/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var travel = await _context.Travels.FindAsync(id);
if (travel == null)
{
return NotFound();
}
travel.IsDelete = true;
//_context.Travels.Remove(travel);
await _context.SaveChangesAsync();
return Ok(travel);
}
private bool TravelExists(long id)
{
return _context.Travels.Any(e => e.ID == id);
}
}
}