How do I do calculated fields on the server of a record being created or updated?

How do I do calculated fields on the server of a record being created or updated?

mike92117mike92117 Posts: 40Questions: 12Answers: 1

I have a very simple table with a few fields. The table has another six or so fields that are calculated on the server and saved as part of a record being created or updated. The calculated fields do not need to be returned to the client, though they can. However, they do need to be calculated on the server and some fields involve numerical methods. These are very fast running. However, I'm unsure how to do this or hook into this with datatables. The DTO class contains all edited fields and all calculated fields. The calculations could be done via a method that returns an instance of the DTO with the calculated fields. i.e, dto = GetCalcValues(dto).

I'm using the .net code for the table that looks like this:

using (var db = new Database(dbType, dbConnection))
{
    var response = new Editor(db, "J")
        .Model<DTO>()
        .Field(new Field("Re")
            .Validator(Validation.Numeric())
        )
        .Field(new Field("J")
            .Validator(Validation.Numeric())
        )
        .TryCatch(false)
        .Process(Request)
        .Data();
    return Json(response);
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,676Questions: 1Answers: 10,497 Site admin
    Answer ✓

    What you want is to use the SetValue() method for the Field class.

    Create the field as normal in C# (don't bother creating it in the Javascript, since the value is irreverent there), then in the PreCreate and PreEdit event handlers, set the value as needed, based on the other values that were submitted.

    There is an example available in the docs here.

    Allan

  • mike92117mike92117 Posts: 40Questions: 12Answers: 1

    thanks - works perfectly

Sign In or Register to comment.