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

Getting the Model with all properties during CRUD, not only the properties with their own column in the Grid

Let's say that I have a Syncfusion grid in my View, and I've enabled CRUD on it. In my Controller, beyond other things, I have:
 

       public ActionResult PerformUpdate(int key, Model value)
        {
            ...perform update...

            return Json(value, JsonRequestBehavior.AllowGet);
        }

For some reasons, not all model properties are assigned to a certain column in the grid. The problem with this approach is that value properties which haven't been assigned to a column in the grid will be 0 or null (depending on the property being a value or reference type), even if the property actually had a value. 

Is there a way to set up the MVC Grid in such a way that returned model object properties always contain their value, even for properties which haven't been assigned to a column (with the syntax col.Field("prop_name").HeaderText("prop_header").Add(); )?



4 Replies

CA Carlo September 2, 2015 09:21 AM UTC

Just a quick note to underline the fact that as of now the only solution I see to this problem is to assign each model property to a grid column, so that the property in the model I get from the view during update always hold the current value. Then, to avoid useless column to be displayed I can set their width at 0 (hiding them doesn't work: that has the same effect as not declaring them at all, in respect to model properties). Isn't there really a better way to accomplish this?


IR Isuriya Rajan Syncfusion Team September 2, 2015 12:48 PM UTC

Hi Carlo,
Thanks for contacting Syncfusion support.
We have achieved your requirement by using the ActionBegin event. This event will trigger for every Grid action before its starts.
We can get the current Grid data by using currentViewData property also and can get the editable data via args.data. By using the extend method, two values can be merged.

<script>

    function ActionBegin(args) {

        if (args.requestType == "save")

            //Extending the two objects

            var data1 = args.model.currentViewData[args.selectedRow];

            $.extend(args.data, data1);       

    }
 </script>



We have created a sample that you can download from following link,
 http://www.syncfusion.com/downloads/support/directtrac/142553/ze/Grid1799785776
Please try the above sample and let us know if it helps. If we misunderstood your query, please provide clear information which will help to provide a prompt solution.

Regards,

Isuriya R



CA Carlo September 3, 2015 07:35 AM UTC

Thanks for your kind answer. The solution you provide is almost right: now, I get all the model properties, even if they haven't been assigned to a column. Unfortunately, properties which are going to be changed by the current edit are overwritten with their old values (from var data1 = args.model.currentViewData[args.selectedRow];). Instead, I want to copy from data1 only properties which aren't present in args.data (which should coincide with the properties from the model which haven't been assigned to a column). I think I met the requisite with the following code, which seems to be working (I briefly tested it). Is that ok in your opinion? 


    function ActionBegin(args) {
        if (args.requestType == "save") {
            
            //retriving all row data
            var allData = args.model.currentViewData[args.selectedRow];

            //extending args.data with the properties from allData
            //which aren't already present in args.data: these are
            //basically the properties which haven't been assigned to
            //a column, and so aren't present in args.data by default
            for (var prop in allData) {
                if (!args.data.hasOwnProperty(prop)) {
                        args.data[prop] = allData[prop];
                }
            }
        }     
    }


BM Balaji Marimuthu Syncfusion Team September 4, 2015 07:17 AM UTC

Hi Carlo,

Yes, the provided code example is correct. You can use it. Please get back to us if you need any further assistance. We will be happy to assist you.

Regards,

Balaji Marimuthu


Loader.
Up arrow icon