We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Perform CRUD operation using anti-forgery token - Empty Row after Insert in UI

Thread ID:

Created:

Updated:

Platform:

Replies:

149288 Nov 20,2019 06:18 PM UTC Nov 21,2019 08:44 AM UTC ASP.NET Core - EJ 2 2
loading
Tags: DataGrid
Sergio Ramos
Asked On November 20, 2019 06:23 PM UTC

Good evening everybody

Situation:

At the moment I'm struggling for hours at the following issue based on: https://ej2.syncfusion.com/aspnetcore/documentation/grid/how-to/perform-crud-operation-using-anti-forgery-token/?_ga=2.187400363.2067813694.1574268221-2069559230.1571128166#perform-crud-operation-using-anti-forgery-token

Everytime when I create an entry over the create dialog. It will be added as an empty row without data. From the server perspective everything is fine. It is saved within the database and after a reload of the page the entry will appear correctly. But how can i solve this issue without reloading the page or adding an action as datasource. I really want to go the lean MVC way.

 

CSHTML:



--> I had issues with pasting in the code.

Controller:


      [HttpGet]
        public async Task Index()
        {
            var model = await  Db.Doctors.OrderBy(m => m.Lastname).ToListAsync();
            return View(model);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task Insert(Doctor value)
        {
            await Db.Doctors.AddAsync(value);
            await Db.SaveChangesAsync();
            return Json(value);
        }


Doctor Class:

    public class Doctor
    {
        public int Id { get; set; }
        [Required]
        public string Firstname { get; set; }
        [Required]
        public string Lastname { get; set; }
    }

Insert Action Response example:

{"id":21,"firstname":"test","lastname":"test"}


Javascript:

My javascript code looks exactly the same as that one suggested on:






Sergio Ramos
Replied On November 20, 2019 07:18 PM UTC

Allright. I fixed it by myself it comes due a change in .net Core 3. There where some changes for json responses.

According to:
https://ej2.syncfusion.com/aspnetcore/documentation/grid/data-binding/#troubleshoot-grid-render-rows-without-data

The following code should be applied:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddJsonOptions(options =>
    {
        options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });
}

But this code Extension doesn't exists anymore in .net core 3.

So the solution is to add the nuget: Microsoft.AspNetCore.Mvc.NewtonsoftJson

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });

https://stackoverflow.com/a/55666898/3313177



Thavasianand Sankaranarayanan [Syncfusion]
Replied On November 21, 2019 08:44 AM UTC

Hi Sergio, 
 
Thanks for your update. 
 
We are happy that the problem has been resolved at your end. 
 
Regards, 
Thavasianand S.  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon