Grid does not POST to server

Hi,
This is my Grid config:

    @{
        var Tool = (new List<string>() { "Search", "Add", "Edit", "Delete", "Update", "Cancel" });
    }
    <SfGrid TValue="Municipality" Toolbar=@Tool AllowPaging="true" AllowSorting="true">
        <GridPageSettings PageSize="10"></GridPageSettings>
        <SfDataManager Url="api/Municipalities" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
        <GridColumns>
            @foreach (var prop in typeof(Municipality).GetProperties())
            {
                <GridColumn Field="@prop.Name" IsPrimaryKey="@(prop.Name == "Id")" AllowEditing="@prop.CanWrite" IsIdentity="@(prop.Name == "Id")" ></GridColumn>
            }
        </GridColumns>
    </SfGrid>

For some reason when I try to add new record it does not POST it to server. Editing works fine. My db has primary key set to autoincrement.

Can you help me?

3 Replies 1 reply marked as answer

DR Dhivya Rajendran Syncfusion Team July 8, 2020 11:07 AM UTC

Hi Milan, 
 
Greeting from Syncfusion Support. 

We have validated the provided information. In your sample, you are using IsIdentity for primary key column. For IsIdentity column, we will not assign any value so its value be considered as null for Primarykey column. We suspect your model property for primary column not accepting null values so that it cause the reported problem.  

We suggest you to specify nullable type to accept the null values or set defaultValue for the primary column to resolve the reported problem.  




Please get back to us if you require further assistance from us. 

Regards, 
R.Dhivya  


Marked as answer

MI Milan July 8, 2020 01:55 PM UTC

Your both proposals cause Grid do POST at least, but I don't like them.
  1. Primary key should not be nullable. If I set it to nullable in my model, EF will try to set Database column to allow nulls and I will get errors
  2. Default value for Primary key is also problematic. It shows default value in disabled cell which is not nice.

I have 2 questions now:
  1. How to hide default value from disabled cell?
  2. Why don't you ignore Identity columns when you generate JSON to post new records to API? That should be proper behavior.


DR Dhivya Rajendran Syncfusion Team July 9, 2020 10:47 AM UTC

Hi Milan, 

Thanks for your update. 

We suggest you to use EditTemplate feature to disable value from disabled cell in Grid. In the below code example, we have render Textbox and based on the action(add or edit) we have applied value for the Textbox and disable it by using enabled property of textbox in EditTemplate.  

You can also use the below way to achieve your requirement. Please refer the below code example and documentation for more information. 

@using WebAPI.Data 
@using Syncfusion.Blazor 
@using Syncfusion.Blazor.Inputs 
@using Syncfusion.Blazor.Data 
@using Syncfusion.Blazor.Grids 
 
<SfGrid TValue="Orders" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" })" AllowSorting="true" AllowPaging="true">  
    <SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager> 
    <GridEvents TValue="Orders" OnActionBegin="ActionBegin"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" IsIdentity="true" TextAlign="TextAlign.Right" Width="120"> 
            <EditTemplate> 
                @{ 
                    var data = (context as Orders); 
                    <SfTextBox Value="@(isAdd ? "" : data.OrderID.ToString())" Enabled="false"></SfTextBox> 
                } 
            </EditTemplate> 
        </GridColumn> 
        . . . . . . 
    </GridColumns> 
</SfGrid> 
 
@code { 
    public bool isAdd { get; set; } = false; 
    public void ActionBegin(ActionEventArgs<Orders> args) 
    { 
        isAdd = args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add) ? true : false; 
    } 
} 


Please get back to us if the provided solution does not meet your requirement. 

Regards, 
R.Dhivya 


Loader.
Up arrow icon