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

Null reference is passed to my controller whenever I am trying to add a new record to the database

When I select a row and click the 'delete' button everything works just fine. The 'delete' action is sent to my controller method (CrudUpdate) successfully. However, when I want to add/update a record, the action reaches my controller but it sends a null reference in the value parameters. How can I fix this? I've attached both my controller and view as well as provided below. Thank you. 



Controller:

[System.Web.Http.HttpPost]

        public ActionResult CrudUpdate([FromBody] ICRUDModel value)

        {

            var db = new ManagementDashboardDbContext();

            using (new ManagementDashboardUnitOfWork())

            {

 

                var entities = GetItems().ToList();

                var mvcModels = new List();

                mvcModels = (List)mvcModels.MapFromObject(entities);

                mvcModels = mvcModels.OrderBy(p => p.PMRSName).ToList();

 

                if (value.action == "update")

                {

                    var id = int.Parse(value.key.ToString());

                    var entityToAdd = db.PMRS.Where(c => c.Id == id).FirstOrDefault();

                    db.PMRS.Add(entityToAdd);

                    db.FinalSaveChanges();

 

                    //PMRSMvcModel val = mvcModels.Where(or => or.Id == ord.Id).FirstOrDefault();

                    //db.FinalSaveChanges(); 

                    return Json(value); 

                }

                else if (value.action == "insert")

                {

                    var id = int.Parse(value.key.ToString());

                    var entityToAdd = db.PMRS.Where(c => c.Id == id).FirstOrDefault();

                    db.PMRS.Add(entityToAdd);

                    db.FinalSaveChanges();

                    //mvcModels.Insert(0, value.value);

                    //db.FinalSaveChanges(); 

                }

                else if (value.action == "remove")

                {

                    var id = int.Parse(value.key.ToString());

 

                    var entityToRemove = db.PMRS.Where(c => c.Id == id).FirstOrDefault();

                    db.PMRS.Remove(entityToRemove);

                    db.FinalSaveChanges();

 

 

                    //mvcModels.Remove(mvcModels.Where(or => or.Id == int.Parse(value.key.ToString())).FirstOrDefault());

                    return Json(value);

                }

                return Json(value, JsonRequestBehavior.AllowGet); 

                //return Json(value.value);

            }

 

        }



View


        @(Html.EJS().Grid("Grid").Width("1250")

                .DataSource(DataManager => { DataManager.Url("/PMRS/UrlDataSource").CrudUrl("/PMRS/CrudUpdate").Adaptor("UrlAdaptor"); })

                .AllowResizing(true)

                .AllowFiltering()

                .AllowSelection()

                .AllowGrouping()

                .AllowMultiSorting()

                .AllowTextWrap()

                .AllowSorting()

                .ToolbarClick("toolbarClick")

                //.ActionBegin("onActionBegin")

 

 

                    .Columns(col =>

                    {

                        //col.Field(p => p.Id).Width("75").IsPrimaryKey(true).IsIdentity(true).Type("number").Add();

                        //col.Field(p => p.Id).Width("75").IsPrimaryKey(true).Type("number").Add();

                        col.Field(p => p.PMRSName).Width("100").Add();

                        col.Field(p => p.ProjDesc).Width("150").Add(); //allow editing(true)

                        col.Field(p => p.PortLead).Width("120").Add();

                        col.Field(p => p.Program).Width("130").Add();

                        col.Field(p => p.AssignedDate).Width("100").Add();

                        col.Field(p => p.ProjStatus).Width("100").Add();

                        col.Field(p => p.CompleteDate).Width("100").Add();

                        col.Field(p => p.CreatedOn).Width("100").Add();

                        col.Field(p => p.CreatedBy).Width("100").Add();

                        col.Field(p => p.ModifiedOn).Width("100").Add();

                        col.Field(p => p.ModifiedBy).Width("100").Add();

                        col.Field(p => p.KSNLink).Width("100").Add();

                        col.Field(p => p.ProjSched).Width("100").Add();

                        col.Field(p => p.Budget).Width("100").Add();

                    })

                    .AllowPaging()

                    .TextWrapSettings(text => { text.WrapMode(Syncfusion.EJ2.Grids.WrapMode.Header); })

                    .PageSettings(page => page.PageCount(2))

                    .EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); })

                    .Toolbar(new List() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })

                    //.SearchSettings()

                    .Render()

 

        )

 

   


Attachment: Archive_b0b4e94a.zip

3 Replies

HJ Hariharan J V Syncfusion Team May 2, 2019 11:36 AM UTC

Hi Hatem, 

Thanks for contacting Syncfusion support. 

We have analyzed the codes which you have shared with us. We would like to inform you that the problem is with the “ICRUDModel” class you are using. We suspect that providing “value” as attribute name may conflict with other fields of the ICRUDModel class itself. So we suggest you to ensure this case in your application. 

We have prepared a sample to perform CRUD action, based on your requirement. We are attaching the sample for your convenience, please download the sample from the link below, 
 
You may try using any other attribute names as like in the below code, 

 
        public ActionResult CrudUpdate([FromBody]ICRUDModel myobjext) 
        { 
            ... 
       } 


Please get back to us if you need further assistance. 

Regards, 
Hariharan 



HM Hatem Muhsen May 7, 2019 05:52 PM UTC

This worked. Thank you so much for the prompt reply. 


HJ Hariharan J V Syncfusion Team May 8, 2019 06:23 AM UTC

Hi Hatem,

Thanks for your update.

We are happy to hear that your problem has been resolved.

Please get back to use if you need any further assitance.

Regards,
Hariharan


Loader.
Live Chat Icon For mobile
Up arrow icon