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

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

Thread ID:

Created:

Updated:

Platform:

Replies:

144344 May 1,2019 06:53 PM UTC May 8,2019 06:23 AM UTC ASP.NET MVC - EJ 2 3
loading
Tags: Grid
Hatem Muhsen
Asked On May 1, 2019 06:58 PM UTC

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

Hariharan J V [Syncfusion]
Replied On 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 


Hatem Muhsen
Replied On May 7, 2019 05:52 PM UTC

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

Hariharan J V [Syncfusion]
Replied On 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


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