Hey guys,
I'm actually new to WebApi and I have a Kendo Grid with the WebApi() option activated:
@(Html.Kendo().Grid<OfferType>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
columns.Command(command => { command.Edit(); });
})
.DataSource(dataSource =>
dataSource
.WebApi()
.Model(model =>
{
model.Id(p => p.ID);
})
.Events(events => events.Error("error_handler"))
.Read(read => read.Url("https://localhost:44362/api/OfferType"))
.Update(update => update.Url("https://localhost:44362/api/OfferType/{0}"))
)
.Pageable()
.Sortable()
)
In your demo für .NET Core you're using the view inside a webapi-project. This is not my case. I'm using the grid in a project which has to be connected to a webapi outside the project/solution. Therefore I have to use "read => read.Url("xyz")" (I think) to get connection to my outside hosted webapi. Because via "read.Action(x,y)" I can't refer to other projects/solutions and urls.
The controller of my webapi is looking like that:
[Route("api/[controller]")]
[ApiController]
public class OfferTypeController : ControllerBase
{
private readonly FrueheHilfenContext _context;
public OfferTypeController(FrueheHilfenContext context)
{
_context = context;
}
[HttpGet]
[EnableCors("AllowLocal")]
public DataSourceResult Get([DataSourceRequest] DataSourceRequest request)
{
List<OfferType> returnList = new List<OfferType>();
using (_context)
{
returnList = _context.OfferTypes.ToList();
}
return returnList.ToDataSourceResult(request);
}
// PUT api/<OfferTypeController>/5
[HttpPut("{id}")]
[EnableCors("AllowLocal")]
public IActionResult Put(int id, OfferType offerType)
{
if (ModelState.IsValid && id == offerType.ID)
{
try
{
using (_context)
{
_context.Entry(offerType).State = EntityState.Modified;
_context.SaveChanges();
}
}
catch (DbUpdateConcurrencyException)
{
return new NotFoundResult();
}
return new StatusCodeResult(200);
}
else
{
return BadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage));
}
}
}
Getting data is working like expected. But I have problems to update my data via put. The console is giving me the errorcode 415 "unsupported media type". When debugging my webapi project the put actions is never been called. So I think there is something wrong with my setup of the grid.
Edit: I know why I'm getting the 415 error (see my answer in the comments). I found out that when I expect a datasourcerequest as an argument in the put-action (so a "[DataSourceRequest] DataSourceRequest offerType" instead of a "OfferType offerType") the action is successfully called without errors. But with the datasourcerequest there aren't any infos about my updated object in the grid. So my grid isn't sending the updated object but the metadata of the grid.
Is anybody seeing my mistake?
Also I try out to change the ".Update()" method to:
.Update(update => update.Url("https://localhost:44362/api/OfferType").Type(HttpVerbs.Put))
and in the webapi-controller
[HttpPut("Update")] public IActionResult Put(OfferType offerType) { ... }
Okay, I found something. Instead of expecting my "OfferType" model in the put action I tried to achieve a DataSourceRequest and then my action is calling!
[HttpPut] public void Put([DataSourceRequest] DataSourceRequest offerType) { }
I also tried to set up a webapi controller in the same project like my view with my grid. I called the controller like described in your demo via
.Update(create => create.Action("Put", "Home", new { id = "{0}"} )))