ifEmpty not working?

ifEmpty not working?

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited July 2020 in Free community support

I have the following in my MVC project:

                    .Field(new Field("CaseActions.BillingAmount")
                        .SetFormatter(Format.IfEmpty(0))
                    )

however, when the postCreate code runs, this field is null and giving me issues in my function

                response.PostCreate += (sender, e) => _LogChange(db, "create", e.Id, e.Values);
                response.PostEdit += (sender, e) => _LogChange(db, "create", e.Id, e.Values);
                response.PostRemove += (sender, e) => _LogChange(db, "create", e.Id, e.Values);

I changed the javascript to add a default value of zero, so that seems to be a band-aid. Why is ifEmpty appear to not be working?

Replies

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    The IfEmpty docs state this:

    Set the value to write to the database if an empty string is submitted from the client-side.

    The value null is not an empty string, ie, "".

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ah, so I would need to use a custom formatter. Seems like that would be a common thing.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    I’m not quite understanding. Are you submitting null from the client-side? If so how? Are you JSON encoding the data you are sending? We send data using HTTP parameters by default, so they are just string values - there is no way to represent types (or indeed null) hence the need for the IfEmpty formatter.

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    sorry, yes, it is blank, not null.

    So, my logging code was hanging on blanks until I added this function:

        public static class Extensions
        {
            public static string NullIfWhiteSpace(this string value)
            {
                if (String.IsNullOrWhiteSpace(value)) { return null; }
                return value;
            }
        }
    

    so now no errors and I don't have to default to a zero anymore.

            private void _LogChange(Database db, string action, object id, Dictionary<string, object> values)
            {
                var data = JsonConvert.SerializeObject(values);
                JObject json = JObject.Parse(data);
                DateTime localDate = DateTime.Now;
    
                db.Insert("CaseActions_History", new Dictionary<string, object>{
                        { "Action", action },
                        //{ "DataValues", JsonConvert.SerializeObject( values ) },
                        { "CaseActionID", id },
                        { "CaseID", json["CaseActions"]["CaseID"].ToString() },
                        { "ActionID", json["CaseActions"]["ActionID"].ToString() },
                        { "BillingAmount", json["CaseActions"]["BillingAmount"].ToString().NullIfWhiteSpace() },
                        { "BilledDate", json["CaseActions"]["BilledDate"].ToString().NullIfWhiteSpace() },
                        { "CaseDefendantID", json["CaseActions"]["CaseDefendantID"].ToString() },
                        { "ActionDate", json["CaseActions"]["ActionDate"].ToString() },
                        { "FollowUpDate", json["CaseActions"]["FollowUpDate"].ToString().NullIfWhiteSpace() },
                        { "ActionStatusID", json["CaseActions"]["ActionStatusID"].ToString() },
                        { "FollowUpStaffID", json["CaseActions"]["FollowUpStaffID"].ToString() },
                        { "ActionNote", json["CaseActions"]["ActionNote"].ToString() },
                        { "CompletedDate", json["CaseActions"]["CompletedDate"].ToString().NullIfWhiteSpace() },
                        { "EnteredBy", json["CaseActions"]["EnteredBy"].ToString() },
                        { "DateUpdated",localDate }
                    });
            }
    

    however, I just realized that it seems .SetFormatter(Format.IfEmpty(0)) is still not working (though I no longer require a zero, so I am going to remove this anyway).

This discussion has been closed.