Hello,
I have the following grid and the Read method is working fine but when I try to delete or edit a record the button doesn't call the method from the controller.
Can you please advise ?
Here is the .cshtml
<kendo-grid name="OverTime">
<toolbar>
<toolbar-button name="create" text="Add new Overtime"></toolbar-button>
</toolbar>
<columns>
<column field="Id" title="ID" />
<column field="RecordDate" format="{0:MM/dd/yyyy}" title="Record Date" />
<column field="TimeFrom" title="Time From" />
<column field="TimeTo" title="Time To" />
<column field="NumberOfHours" title="Number of hours" />
<column field="OvertimeReason" title="Overtime Reason" />
<column>
<commands>
<column-command text="delete" name="destroy"></column-command>
<column-command text="update" name="update"></column-command>
</commands>
</column>
</columns>
<datasource type="DataSourceTagHelperType.Ajax" page-size="20">
<schema>
<model id="Id">
<fields>
<field name="RecordDate" type="date" />
<field name="TimeFrom" type="time" />
<field name="TimeTo" type="time" />
<field name="NumberOfHours" type="number" />
<field name="OvertimeReason" type="string" />
</fields>
</model>
</schema>
<transport>
<read url="@Url.Action("Read_Overtime","OverTimes", new { id = ViewContext.RouteData.Values["Id"] })" />
<destroy url="@Url.Action("Destroy_Overtime","OverTimes")" />
<update url="@Url.Action("Update_Overtime","OverTimes")" />
</transport>
</datasource>
<groupable enabled="true" />
<sortable enabled="true" />
<pageable button-count="5" refresh="true" />
<filterable enabled="true" />
<editable enabled="true" />
<toolbar>
</toolbar>
</kendo-grid>
Here is the update and delete method from the controller.
[HttpPost]
public ActionResult Destroy_Overtime ([DataSourceRequest]DataSourceRequest request, OverTimeViewModel overTime)
{
try
{
OverTime over = _context.OverTime.Find(overTime.Id);
if (over == null)
{
return Json("Employee Not Found");
}
_context.OverTime.Remove(over);
_context.SaveChanges();
return Json(_context.OverTime.ToList());
}
catch (Exception ex)
{
return Json(ex.Message);
}
}
[HttpPost]
public ActionResult Update_Overtime([DataSourceRequest]DataSourceRequest request, OverTime overTime)
{
try
{
if(ModelState.IsValid)
{
_context.Entry(overTime).State = EntityState.Modified;
_context.SaveChanges();
return Json(new[] { overTime }.ToDataSourceResult(request, ModelState));
}
else
{
return Json(_context.OverTime.ToList());
}
}
catch(Exception ex)
{
return Json(ex.Message);
}
}