Hi!
Im using a Grid component InCell Editing the OnDelete, OnUpdate handlers are working fine but OnCreate handler its not working. By the way im using a service to manage the CRUD operations as follows
Page Component
@page "/districts"
@using MVC.Services
@using MVC.Models
@using System.ComponentModel.DataAnnotations
@inject IDistrictService DistrictService
<h3>Districts</h3>
<TelerikGrid Data="@district" Sortable="true" EditMode="@GridEditMode.Incell"
Height="500px"
Pageable="true" PageSize=@PageSize
OnUpdate=@UpdateItem OnDelete=@DeleteItem OnCreate=@CreateItem OnCancel="@OnCancelHandler">
<GridToolBar>
<GridCommandButton Command="Add" Icon="add">Add District</GridCommandButton>
</GridToolBar>
<GridColumns>
<GridColumn Field="@(nameof(District.Id))" Editable="false" />
<GridColumn Field="@(nameof(District.Description))" Title="Description" />
<GridColumn Field="@(nameof(District.EnableApprovalWorkflow))" Title="Enable Approval Workflow" />
<GridCommandColumn>
<GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
</GridCommandColumn>
</GridColumns>
</TelerikGrid>
@code {
int PageSize = 15;
IEnumerable<District> district;
protected override async Task OnInitializedAsync()
{
await GetGridData();
}
async Task GetGridData()
{
district = await DistrictService.DistrictList();
}
async Task CreateItem(GridCommandEventArgs args)
{
District item = (District)args.Item;
await DistrictService.DistrictInsert(item);
await GetGridData();
}
void OnCancelHandler(GridCommandEventArgs args)
{
District item = (District)args.Item;
}
async Task DeleteItem(GridCommandEventArgs args)
{
District item = (District)args.Item;
await DistrictService.DistrictDelete(item.Id);
await GetGridData();
}
async Task UpdateItem(GridCommandEventArgs args)
{
District item = (District)args.Item;
await DistrictService.DistrictUpdate(item);
await GetGridData();
}
}
Service Logic
using Dapper;
using Microsoft.Data.SqlClient;
using MVC.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVC.Services
{
public class DistrictService : IDistrictService
{
private readonly SqlConnectionConfiguration _configuration;
public DistrictService(SqlConnectionConfiguration configuration)
{
_configuration = configuration;
}
public async Task<bool> DistrictInsert(District district)
{
using (var conn = new SqlConnection(_configuration.Value))
{
var parameters = new DynamicParameters();
parameters.Add("Description", district.Description, DbType.String);
parameters.Add("EnableApprovalWorkflow", district.EnableApprovalWorkflow, DbType.Boolean);
await conn.ExecuteAsync("spLookupDistrict_Insert", parameters, commandType: CommandType.StoredProcedure);
}
return true;
}
public async Task<IEnumerable<District>> DistrictList()
{
IEnumerable<District> districts;
using (var conn = new SqlConnection(_configuration.Value))
{
districts = await conn.QueryAsync<District>("spLookupDistrict_List", commandType: CommandType.StoredProcedure);
}
return districts;
}
public async Task<IEnumerable<District>> DistrictSearch(string @Param)
{
var parameters = new DynamicParameters();
parameters.Add("@Param", Param, DbType.String);
IEnumerable<District> districts;
using (var conn = new SqlConnection(_configuration.Value))
{
districts = await conn.QueryAsync<District>("spLookupDistrict_Search", parameters, commandType: CommandType.StoredProcedure);
}
return districts;
}
public async Task<District> District_GetOne(int @Id)
{
District district = new District();
var parameters = new DynamicParameters();
parameters.Add("@Id", Id, DbType.Int32);
using (var conn = new SqlConnection(_configuration.Value))
{
district = await conn.QueryFirstOrDefaultAsync<District>("spLookupDistrict_GetOne", parameters, commandType: CommandType.StoredProcedure);
}
return district;
}
public async Task<bool> DistrictUpdate(District district)
{
using (var conn = new SqlConnection(_configuration.Value))
{
var parameters = new DynamicParameters();
parameters.Add("Id", district.Id, DbType.Int32);
parameters.Add("Description", district.Description, DbType.String);
parameters.Add("EnableApprovalWorkflow", district.EnableApprovalWorkflow, DbType.Boolean);
await conn.ExecuteAsync("spLookupDistrict_Update", parameters, commandType: CommandType.StoredProcedure);
}
return true;
}
public async Task<bool> DistrictDelete(int Id)
{
var parameters = new DynamicParameters();
parameters.Add("@Id", Id, DbType.Int32);
using (var conn = new SqlConnection(_configuration.Value))
{
await conn.ExecuteAsync("spLookupDistrict_Delete", parameters, commandType: CommandType.StoredProcedure);
}
return true;
}
}
}