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

CRUDModel param/value is always null or empty

Hello

I am using ASP.NET CORE EJ 2 version 16.3.0.27, Microsoft.AspNetCore.MVC and Microsoft.EntityFrameworkCore .
Using the grid with the UrlAdaptor.

When editing records, the corresponding url action is called correctly and the update works fine,
but at insert the param / value is always null or empty.

Could you provide me to solve this problem?

[View]
@model IEnumerable<AWT_Admin.Models.TblMasterBuilding>

<ejs-grid id="Grid_Buildings" toolbar="@(new List<string>() { "Add", "Edit", "Update", "Delete", "Cancel", "Search" })" actionComplete="onActionComplete" allowPaging="true" allowSorting="true" allowFiltering="false" allowGrouping="false">
    <e-data-manager url="/TblMasterBuildings/UrlDatasource" updateUrl="/TblMasterBuildings/RecordUpdate" insertUrl="/TblMasterBuildings/RecordInsert" adaptor="UrlAdaptor"></e-data-manager>
    <e-grid-pagesettings pageCount="5" pageSizes="@(new string[] { "12", "24", "48" , "All" })"></e-grid-pagesettings>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="BuildingId" headerText="Gebäude ID" allowEditing="false" isPrimaryKey="true" isIdentity="true" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="Building" headerText="Gebäude" width="150"></e-grid-column>
        <e-grid-column field="Active" headerText="Aktiv" displayAsCheckBox="true" type="boolean" editType="booleanedit" minWidth="8" width="120"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function onActionComplete(args) {
        if (args.requestType === "delete" || args.requestType === "save") {
            this.refresh();
        }
    };
</script>

[Model]
    public partial class TblMasterBuilding
    {
        public TblMasterBuilding()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TblMasterBuilding"/> class.
        /// </summary>
        public TblMasterBuilding(int BuildingId, string Building, bool? Active)
        {
            this.BuildingId = BuildingId;
            this.Building = Building;
            this.Active = Active;

            TblLocation = new HashSet<TblLocation>();
        }

        [Key]
        public int BuildingId { get; set; }
        public string Building { get; set; }
        public bool? Active { get; set; }

        public ICollection<TblLocation> TblLocation { get; set; }
    }

[Contoller]
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AWT_Admin.Models;
using Syncfusion.EJ2.Base;
using Newtonsoft.Json;

        public IActionResult RecordUpdate([FromBody]CRUDModel<TblMasterBuilding> param)
        {
            _context.Entry(param.Value).State = EntityState.Modified;
            _context.SaveChanges();

            return Json(param);
         }

        public IActionResult RecordInsert([FromBody]CRUDModel<TblMasterBuilding> param)
        {
             _context.Add(param);
            _context.SaveChanges();

            return Json(param);
        }


Also a complete CRUD example for AspNetCore.Mvc, EntityFrameworkCore and the current Syncfusion.EJ2.Base
including source code for model, view and controller, would be very nice.

Thanks in advance
Christian

3 Replies

MS Madhu Sudhanan P Syncfusion Team November 8, 2018 04:07 AM UTC

Hi Christian, 

Thanks for contacting Syncfusion support. 

From the code example, we noticed that you have set BuildingId column as isIdentity in grid columns hence that particular value will not be send to the server during add operation as it will be generated by the server itself and the model property BuildingId is not nullable and hence it failed to bind the values. 

To resolve this issue, either you can change the model propery as Nullable


[Key] 
public Nullable<int> BuildingId { get; set; } 


Else send any dummy value for the field BuildingId while adding record from the grid. 


<ejs-grid id="Grid_Buildings" actionBegin="actionBegin"> 
    ... 
</ejs-grid> 
 
<script> 
    function actionBegin(args) { 
        if (args.requestType === "save" && args.action === "add") { 
            args.data['BuildingId'] = 0; 
        } 
    }; 
</script> 


Regards, 
Madhu Sudhanan P 



CH Christian Haas November 8, 2018 08:38 AM UTC

Hello,

thanks for your quick help. You saved my day. :-)
The problem is solved. Many thanks.

Many Greetings,
Christian


VA Venkatesh Ayothi Raman Syncfusion Team November 9, 2018 04:05 AM UTC

Hi Christian, 

Thanks for the feedback. 

We are very happy to hear that your requirement is achieved. 

Regards, 
Venkatesh Ayothiraman. 


Loader.
Up arrow icon