Cant Add/Edit/Delete
Good day. I'm having an issue on CRUD using the code below. IM using EF Core. When im removing the datasource I can add records. But I cant edit or update because no records shows on the grid. But if I have datasource the grid shows records but then i add / edit / delete records doesnt work. TIA
pls see my code below
@page "/api/ranks"
@using System.Net.Http
@using Syncfusion.Blazor
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Grids
@using SPMSApp.Shared
@inject HttpClient Http
<h3>Rank</h3>
<SfGrid TValue="Rank" DataSource="@ranks" Toolbar="@(new List<string>() { "Add", "Edit", "Delete"})">
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog"></GridEditSettings>
<SfDataManager Url="api/Rank" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
<GridColumns>
<GridColumn Field="RankId" HeaderText="Id" IsPrimaryKey="true" IsIdentity="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="Name" HeaderText="Name" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="ShortName" HeaderText="Short Name" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="Description" HeaderText="Description" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="Type" HeaderText="Type" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code {
private List<Rank> ranks { get; set; }
protected override async Task OnInitializedAsync()
{
ranks = await Http.GetFromJsonAsync<List<Rank>>("api/Rank");
}
SIGN IN To post a reply.
3 Replies
RN
Rahul Narayanasamy
Syncfusion Team
July 22, 2020 01:44 PM UTC
Hi Matt,
Greetings from Syncfusion.
Query: But if I have datasource the grid shows records but then i add / edit / delete records doesnt work.
We have validated your query with the provided information and you have used both DataSource property and SfDataManager to bind the data to the Grid. You should use either one of the way(using DataSource or SfDataManager) to bind the data to the Grid.
Also, we have prepared a sample based on your requirement. The CRUD operations are working fine without any issues. Find the below code snippets and sample for your reference.
|
<SfGrid @ref="Grid" TValue="Orders" AllowFiltering="true" AllowSorting="true" AllowPaging="true"
Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
<SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
<GridColumns>
<GridColumn Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field="Freight" HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
|
Reference:
Please let us know if you have any concerns.
Regards,
Rahul
Hi Matt,
Greetings from Syncfusion.
Query: But if I have datasource the grid shows records but then i add / edit / delete records doesnt work.
We have validated your query with the provided information and you have used both DataSource property and SfDataManager to bind the data to the Grid. You should use either one of the way(using DataSource or SfDataManager) to bind the data to the Grid.
Also, we have prepared a sample based on your requirement. The CRUD operations are working fine without any issues. Find the below code snippets and sample for your reference.
<SfGrid @ref="Grid" TValue="Orders" AllowFiltering="true" AllowSorting="true" AllowPaging="true"Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })"><GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings><SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager><GridColumns><GridColumn Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn><GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150"></GridColumn><GridColumn Field="Freight" HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn></GridColumns></SfGrid>
Reference:
Please let us know if you have any concerns.
Regards,Rahul
Good day Rahul,
I followed your instruction. But still I cant see any record from the grid. Yes I can add record, but cant update because the grid is blank.
Kindly see below my Controller.( RankController)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SPMSApp.Server.IRepositories;
using SPMSApp.Server.Repositories;
using SPMSApp.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace SPMSApp.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RankController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
public RankController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Rank>>> GetAllRankAsync()
{
return Ok(await _unitOfWork.Rank.GetAllAsync());
}
[HttpGet("{id:int}")]
public async Task<ActionResult<Rank>> FindRankByIdAsync(int id)
{
var result = await _unitOfWork.Rank.FindByIdAsync(x => x.RankId == id);
if (result == null)
{
return NotFound();
}
return result;
}
[HttpPost]
public async Task<ActionResult<Rank>> Post([FromBody] Rank rank)
{
try
{
Rank r = new Rank();
{
r.Description = rank.Description;
r.Name = rank.Name;
r.ShortName = rank.ShortName;
r.Type = rank.Type;
}
_unitOfWork.Rank.Add(r);
await _unitOfWork.SaveChangesAsync();
return Ok();
}
catch (Exception)
{
return NotFound();
}
}
[HttpPut("{id:int}")]
public async Task<ActionResult<Rank>> Put(int id, [FromBody] Rank rank)
{
try
{
var query = _unitOfWork.Rank.FindById(x => x.RankId == id);
query.Description = rank.Description;
query.Name = rank.Name;
query.ShortName = rank.ShortName;
query.Type = rank.Type;
_unitOfWork.Rank.Update(query);
await _unitOfWork.SaveChangesAsync();
return Ok();
}
catch (Exception)
{
return NotFound();
}
}
[HttpDelete("{id:int}")]
public async Task<IActionResult> Delete(int id)
{
try
{
var result = _unitOfWork.Rank.FindById(x=> x.RankId == id);
_unitOfWork.Rank.Remove(result);
await _unitOfWork.SaveChangesAsync();
return Ok();
}
catch (Exception)
{
return NotFound();
}
}
}
}
Thank you,
Matt
JP
Jeevakanth Palaniappan
Syncfusion Team
July 24, 2020 04:09 PM UTC
Hi Matt,
Thanks for the update.
We have validated your problem with the provided information but still we could not reproduce the reported problem. So we need some information regarding the reported problem. Could you please share the below details that will be helpful for us to provide better solution.
- Whether did you face any console errors. If yes, please share those details.
- Bind the ActionFailure event to the grid, if you get any error in that event share the details of the issue with us.
Documentation Link - https://blazor.syncfusion.com/documentation/datagrid/events/#onactionfailure
- Please share the Syncfusion nuget version details.
- If possible, please share the issue reproducing sample.
Regards,
Jeevakanth SP.
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
-
MM matt martinez
- Jul 21, 2020 11:42 AM UTC
- Jul 24, 2020 04:09 PM UTC