Hi,
I have to send some additional parameters in the header of my request for authentication purposes for which I need to call the before send function. As there is no direct way to override this method in Razor so I have added a script after the @Html.Kendo().Grid() but the issue is that the first time the request is sent before the script is executed. Below is my code snippet
@(Html.Kendo().Grid<TEST.ViewModel>()
.Name("Grid")
.TableHtmlAttributes(new {Class="kendoGrid"})
.Columns(columns =>
{
columns.Bound(a => a.ContactPerson)
.Title("Name");
columns.Bound(a => a.CellPhoneNumber)
.Title("Mobile Number");
columns.Command(c =>
{
c.Edit();
c.Destroy();
});
})
.Sortable()
.Pageable()
.Filterable()
.Resizable(resize => resize.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(a => a.Id);
})
.Read(read => read.Url("../api/User").Type(HttpVerbs.Get))
.Destroy(destroy => destroy.Url("../api/User").Type(HttpVerbs.Delete))
)
)
<script>
$(function () {
var grid = $("#Grid").data("kendoGrid");
grid.dataSource.transport.options.read.beforeSend = function (req) {
req.setRequestHeader('Authorization', 'Basic abc:123');
};
grid.dataSource.transport.options.destroy.beforeSend = function (req) {
req.setRequestHeader('Authorization', 'Basic abc:123');
};
// WebAPI needs the ID of the entity to be part of the URL e.g. DELETE /orionws/Agent/80
grid.dataSource.transport.options.destroy.url = function (data) {
return "../api/User/" + data.Id;
};
});
</script>
I have to send some additional parameters in the header of my request for authentication purposes for which I need to call the before send function. As there is no direct way to override this method in Razor so I have added a script after the @Html.Kendo().Grid() but the issue is that the first time the request is sent before the script is executed. Below is my code snippet
@(Html.Kendo().Grid<TEST.ViewModel>()
.Name("Grid")
.TableHtmlAttributes(new {Class="kendoGrid"})
.Columns(columns =>
{
columns.Bound(a => a.ContactPerson)
.Title("Name");
columns.Bound(a => a.CellPhoneNumber)
.Title("Mobile Number");
columns.Command(c =>
{
c.Edit();
c.Destroy();
});
})
.Sortable()
.Pageable()
.Filterable()
.Resizable(resize => resize.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(a => a.Id);
})
.Read(read => read.Url("../api/User").Type(HttpVerbs.Get))
.Destroy(destroy => destroy.Url("../api/User").Type(HttpVerbs.Delete))
)
)
<script>
$(function () {
var grid = $("#Grid").data("kendoGrid");
grid.dataSource.transport.options.read.beforeSend = function (req) {
req.setRequestHeader('Authorization', 'Basic abc:123');
};
grid.dataSource.transport.options.destroy.beforeSend = function (req) {
req.setRequestHeader('Authorization', 'Basic abc:123');
};
// WebAPI needs the ID of the entity to be part of the URL e.g. DELETE /orionws/Agent/80
grid.dataSource.transport.options.destroy.url = function (data) {
return "../api/User/" + data.Id;
};
});
</script>