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

3

I'm having a validation difficulty. I have View that sends to Controller information, where view is used knockoutjs , however it has time it sends null or "null" or "NaN" or "false" or "0" , I have not seen "undefined" yet!

Script part knockoutjs when 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: do I have to do something generic or do I have to check it one at a time?

    
asked by anonymous 09.08.2017 / 16:21

2 answers

2

You can try this

        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!");
            }
        }
    
11.08.2017 / 06:53
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 the following form:

    criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM =  
 ValidNull(criticalAnalysisRecord.InternalAuditRecordTopics.ElementAt(i).OM);
    
11.08.2017 / 13:44