Record is Added Twice

I am having a problem when I add a new record to the Grid with the Add Button.  One record is added to the grid with no primary key and one is which is added to the database which generates the primary key and both are shown in the Grid.


  <SfGrid DataSource="@model" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Search" })" AllowPaging="true">

            <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog" ShowDeleteConfirmDialog="true">

                <HeaderTemplate>

                    @{

                        var text = context as MemberLookupModel;

                        if (string.IsNullOrEmpty(@text.Term))

                        {

                            <span>Add New Term</span>

                        }

                        else

                        {

                            <span>Edit @text.Term</span>

                        }

                    }

                </HeaderTemplate>

            </GridEditSettings>

            <GridEvents OnActionBegin="LookupsOnBeginHandler" TValue="MemberLookupModel"></GridEvents>

            <GridColumns>

                <GridColumn Field=@nameof(MemberLookupModel.MemberLookupId) IsPrimaryKey="true" Visible="true"></GridColumn>

                <GridColumn Field=@nameof(MemberLookupModel.LookupType) EditType="EditType.DropDownEdit" HeaderText="Lookup Type">

                    <EditTemplate>

                        <SfComboBox TValue="string" TItem="AppLookupType" Autofill="true" Placeholder="Select Category"

                                    DataSource="@modelLookupTypes" AllowFiltering="true" @bind-Value="@((context as MemberLookupModel).LookupType)">

                            <ComboBoxFieldSettings Text="LookupType" Value="LookupType" />

                        </SfComboBox>


                    </EditTemplate>


                </GridColumn>

                <GridColumn Field=@nameof(MemberLookupModel.Term)></GridColumn>

            </GridColumns>

        </SfGrid>



IList model;

IEnumerable modelLookupTypes;


async Task LookupsOnBeginHandler(ActionEventArgs Args)

{

//await Task.Yield();

if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save)

{

if (Args.Action == "Add")

{

await InsertMemberLookup(Args.Data);

}

else

{

await UpdateMemberLookup(Args.Data);

}

}

if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete)

{

await DeleteMemberLookup(Convert.ToInt32(Args.Data.MemberLookupId));

}

return;

}


async Task InsertMemberLookup(MemberLookupModel model)

{

var entity = new MemberLookup();


entity.Global = false;

entity.Id = _userId;

entity.LookupType = model.LookupType;

entity.Term = model.Term;


var (result, returnedEntity) = await lookupService.MemberLookupAdd(entity);

if (result.ResultType == ResultType.Success)

{

ToastService.ShowSuccess("Record has been saved.", "Success!");

}

else

{

ToastService.ShowError(result.Description, "Error!");

}

}


public class MemberLookupModel

{


[Key]

public int? MemberLookupId { get; set; }


[Required, StringLength(50)]

public string LookupType { get; set; }


[Required, StringLength(50)]

public string Term { get; set; }

}


1 Reply

RN Rahul Narayanasamy Syncfusion Team April 5, 2022 04:20 AM UTC

Hi Charles,


Greetings from Syncfusion.


We need some details regarding your reported query. Could you please share the below details which will be helpful to validate and provide a better solution?


  • Video demonstration of the problem.
  • Whether did you generate the primary key column value automatically from your database? If yes, then please set IsIdentity as true to the primary key column. If the primary key column value is Auto Incremented in your database, kindly define the IsIdentity property of GridColumn as true for the PrimaryKey column.  

Refer to our UG documentation for your reference: https://blazor.syncfusion.com/documentation/datagrid/editing - Notes section after the code example.

  • Share a simple reproducible sample if possible.


Regards,

Rahul



Loader.
Up arrow icon