Hello,
i am working on ASP.NET CORE with razor page.
I have a grid with CRUD operation (UrlAdaptor).
I am able to bind data, filter and execute CRUD.
My problem is related to the bind inside OnPostLoadData because i want to filter the datasource with the Route Data parameters.
I am unable to retrieve the Route Data inside OnPostLoadData i think because the ajax request doesn't allow it.
How i can solve it? maybe pass additional parameter on PostLoadData? How?
Thanks
CSHTML code:
@page "{Route1?}"
@model Test.Pages.Gridmodel_sede_installazione
@using Syncfusion.EJ2;
@Html.AntiForgeryToken()
<ejs-grid id="grid" dataBound="dataBound" allowFiltering="true" allowSorting="true" allowResizing="true" allowPaging="true"
showColumnChooser="true" showColumnMenu="true" load="onload"
toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update", "Search" })"
gridLines="Both">
<e-data-manager url="/Grid_sede_installazione?handler=LoadData" insertUrl="/Grid_sede_installazione?handler=Insert"
updateUrl="/Grid_sede_installazione?handler=Update" removeUrl="/Grid_sede_installazione?handler=Delete" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-filterSettings type="Menu"></e-grid-filterSettings>
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog"></e-grid-editSettings>
<e-grid-columns>
<e-grid-column field="Column1" headerText="Column1" visible="false" isidentity="true" isPrimaryKey="true" width="120"></e-grid-column>
<e-grid-column field="Column2" headerText="Column2" validationRules="@(new { required=true})" width="120"></e-grid-column>
<e-grid-column field="Column3" headerText="Column3" width="120"></e-grid-column>
@*default value with Route Data? defaultValue="(@RouteData.Values["PartitaIva"].ToString())"*@
..........
</e-grid-columns>
</ejs-grid>
}
<script>
function onLoad(args) {
this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
function dataBound(args) {
this.autoFitColumns();
}
CS code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Web;
using Syncfusion.EJ2.Base;
using System.Collections;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore;
namespace Test.Pages
{
[IgnoreAntiforgeryToken(Order = 1001)] // This is necessary to trigger the method OnPostLoadData and OnPostUpdateData
public class Gridmodel_sede_installazione : PageModel
{
//carico il collegamento al database
public Context context = new Context (); //EntityFrameworkCore Postgres
[BindProperty(SupportsGet = true)] //in alternativa public void OnGet(string Route1)
public string Route1{ get; set; }
public void OnGet() { }
public JsonResult OnPostLoadData([FromBody] DataManagerRequest dm)
{
IEnumerable data;
if (Route1== null)
{
data = context.Anagrafica.ToList();
}
else
{
data = context.Anagrafica.Where(s => s.Column6== Route1).ToList();
}
//fa tutto in automatico, filtri, ordinamento, restituzione dati, ...
int count = context.Anagrafica.Count();
DataOperations operation = new DataOperations();
data = operation.Execute(data, dm);
return new JsonResult(new { result = data, count = count }, new JsonSerializerSettings());
}
public JsonResult OnPostInsert([FromBody]CRUDModel<Anagrafica> value)
{
context.Anagrafica.Add(value.Value);
context.SaveChanges();
return new JsonResult(value.Value);
}
public JsonResult OnPostUpdateAsync([FromBody]CRUDModel<Anagrafica> value)
{
var entity = context.Anagrafica.AsNoTracking().FirstOrDefault(s => s.Column6== value.Value.Column6);
if (entity != null)
{
context.Anagrafica.Update(value.Value);
context.SaveChanges();
}
return new JsonResult(value.Value);
}
public JsonResult OnPostDelete([FromBody]CRUDModel<Anagrafica> value)
{
var entity = context.Anagrafica.FirstOrDefault(s => s.Column6== value.Key.ToString());
if (entity != null)
{
context.Anagrafica.Remove(entity);
}
context.SaveChanges();
return new JsonResult(value);
}
}
}