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. Image for the cookie policy date

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

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:






2 Replies

SR Sergio Ramos 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




TS Thavasianand Sankaranarayanan Syncfusion Team 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.  


Loader.
Live Chat Icon For mobile
Up arrow icon