calculating fields editor .net libraries

calculating fields editor .net libraries

SstephensSstephens Posts: 2Questions: 1Answers: 0
edited November 2016 in Free community support

Hi,

I am new to datatables editor. I am trying to write a server side function that takes input values, and calculates another value, writes it to the db, and returns the value in the json.

I have tried to do something like:

var response =
                new Editor(db, "Item", "ItemID")

                .Model<ItemGenModel1>()
. 
.
.
.
response.PostEdit += (sender, e) =>
                {
                    //get a value
                    decimal w = e.Editor.Field("width").GetValue();
                    //set a value
                    e.Editor.Field("cubed").SetValue(w*w*w);
                };

The GetValue returns a null dynamic object. I obviously am missing something. I was not able to find any examples that helped.

Any guidance would be appreciated.

Thanks,

Scott

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin

    Hi Scott,

    What you have looks very close, but you need to use PreEdit rather than PostEdit. The PostEdit code is run after the values have been written to the database.

    The other issue is that Field().GetValue() is not the method to use to get the POSTed value (that method is used for data that should be populated to the client-side (i.e. DataTables' get request).

    Use e.Values["width"] instead. You'll probably need to cast it to be an decimal as well, since it is stored as an object ((decimal)e.Values["width"] should do it).

    Allan

  • SstephensSstephens Posts: 2Questions: 1Answers: 0

    Hi Allan,

    Thanks. That pointed me in the right direction. In my initial question, I didn't go into a lot of detail. The actual data structure uses a number of left joins, so e.Values["Item"] was the actual call. From there, I used a generic dictionary object to get at the value. I ended up writing a function getvalues() to query the values directly from the database that I need for the actual calculations. I am not sure if this is the most efficient, but it works. If there is a better way, I am all eyes. Thanks again.

    Dictionary<string,object> dictItem = (Dictionary <string,object> )e.Values["Item"];
                        object outCwidth;
                        dictItem.TryGetValue("CartonODWidth", out outCwidth);
                        if (outCwidth != null)
                        {
                            decCartonWidth = Convert.ToDecimal(outCwidth);
                        } else
                        {
                           var cwidth = getvalues("CartonODWidth", itemid);
                            if (cwidth != null)
                            {
                                decCartonWidth = Convert.ToDecimal(cwidth);
                            }
                        }
    
This discussion has been closed.