C# check "null" or "Nan" or "false" or "0"

Asked

Viewed 312 times

3

I’m having a validation difficulty. I have a View that sends to the Controller information, where in view is used knockoutjs, but there is time that he sends a null or "null" or "NaN" or "false" or "0", I just didn’t see "undefined" yet!

Part of Script knockoutjs when you have no information:

  var InternalAuditRecordTopicEmpty = {
            InternalAuditRecordTopicID: 0,
            InternalAuditRecordID: 0,
            Name:'',
            Order:9999,
            Request:'',
            NonComformityType:'',
            Comformity:'',
            OM:'',
            Observation:'',
            Evidences:'',
        };

My Controller:

     if (criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM == "false"   
        || criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM == "0")  
                                   criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM = null;

if (criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM == null && iscontrol)
                        { error =true; }

My question is: is there any way I can do something generic or have to check one by one?

2 answers

2


You can try to do that

        string[] ElementAt = { "null", "NaN", "false", "0", "undefined", null };
        string erro = null;

        foreach (var item in ElementAt)
        {
            if (erro == item)
            {
                MessageBox.Show("Ops, um erro aconteceu!");
            }
        }

1

Well I did so to try to solve this problem:

I created a Method:

 public string ValidNull(string val)
        {
            switch (val)
            {
                case "null":return null;
                case "NaN": return null;
                case "false":return null;
                case "0":return null;
                case "undefined": return null;
                default:return val;
            }

        }

I used it as follows:

    criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM =  
 ValidNull(criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.