2021年3月11日星期四

Can't add a row in SFGrid when Database table is empty

I have a SfGrid which doesn't allow me to add new rows(the add button is shown as clicked, but it doesn't display the input row) if the database table is empty. No errors are thrown. If I manually insert a row in the SSMS(where my database is), the row is shown and I can add new rows normally(everything works). I'm using Syncfusion for my Blazor-WebAssembly project.

Here is the SfGrid component in my razor page:

<SfGrid TValue="Note" Toolbar="@(new List<string>() { "Add", "Edit", "Update", "Cancel" })">      <GridEvents OnActionBegin="OnBeginHandler" CommandClicked="@CommandClickHandler" TValue="Note"></GridEvents>      <SfDataManager Url="/api/Notes" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>      <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>      <GridColumns>          <GridColumn Field="@nameof(Note.NoteId)" IsPrimaryKey="true" Visible="false"></GridColumn>          <GridColumn Field="@nameof(Note.Name)" ValidationRules="@(new ValidationRules(){Required=true})"></GridColumn>          <GridColumn>              <GridCommandColumns>                  <GridCommandColumn Type="CommandButtonType.Delete" ButtonOption="@(new CommandButtonOptions() { IconCss = "e-icons e-delete", CssClass = "e-flat" })"></GridCommandColumn>              </GridCommandColumns>          </GridColumn>      </GridColumns>  </SfGrid>   

And the @code part of the page:

private void OnBeginHandler(ActionEventArgs<Note> args)  {      if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save)      {          if (args.Action == "add")          {              var t = args.Data;          }      }  }    public void CommandClickHandler(CommandClickEventArgs<Note> args)  {      if (args.CommandColumn.Type != CommandButtonType.Save)          return;        var lookup = args.RowData;        StateHasChanged();  }  

And finally, the NotesController :

[Route("api/[controller]")]  [ApiController]  public class NotesController : ControllerBase  {      private readonly OrganizatorContext _context;      public NotesController(OrganizatorContext context)      {          _context = context;      }        // GET: api/<NotesController>      [HttpGet]      public object Get()      {          return new { Items = _context.Notes, Count = _context.Notes.Count() };      }      // GET api/<NotesController>/5        [HttpGet("{id}")]      public object Get(long id)      {          return new { Items = _context.Notes.Where(x => x.NoteId.Equals(id)).FirstOrDefault(), Count = _context.Notes.Count() };      }        // POST api/<NotesController>      [HttpPost]      public void Post([FromBody] Note note)      {          _context.Notes.Add(note);          _context.SaveChanges();      }        // PUT api/<NotesController>/5      [HttpPut("{id}")]      public void Put(long id, [FromBody] Note note)      {          Note note1 = _context.Notes.Where(x => x.NoteId.Equals(note.NoteId)).FirstOrDefault();          note1.Name = note.Name;          note1.IsDone = note.IsDone;          note1.Due = note.Due;          _context.SaveChanges();      }        // DELETE api/<NotesController>/5      [HttpDelete("{id}")]      public void Delete(long id)      {          Note note1 = _context.Notes.Where(x => x.NoteId.Equals(id)).FirstOrDefault();          _context.Notes.Remove(note1);          _context.SaveChanges();      }  }  
https://stackoverflow.com/questions/66546568/cant-add-a-row-in-sfgrid-when-database-table-is-empty March 09, 2021 at 08:05PM

没有评论:

发表评论