0

first class

public class DBData
    {
        public int Size { get; set; }
        public string Key { get; set; }
        public object Value { get; set; }
        public bool IsRequired { get; set; }
        public SqlDbType ValueType { get; set; }

        public DBData(string Key, int Size, SqlDbType ValueType, bool IsRequired = false)
        {
            this.Key = Key;
            this.Size = Size;
            this.ValueType = ValueType;
            this.IsRequired = IsRequired;
        }
    }

2nd class

public class ApplicantData
    {
        // NSWS Param
        public DBData ApprovalId = new DBData("ApprovalId", 255, SqlDbType.VarChar, true);
        public DBData InvestorSWSId = new DBData("InvestorSWSId", 255, SqlDbType.VarChar, true);
        public DBData InvestorReqId = new DBData("InvestorReqId", 255, SqlDbType.VarChar, true);

        // Basic Param
        public DBData ID = new DBData("ID", 255, SqlDbType.VarChar, true);
        public DBData Name = new DBData("Name", 255, SqlDbType.VarChar, true);
        public DBData Email = new DBData("Email", 255, SqlDbType.VarChar, true);
        public DBData Mobile = new DBData("Mobile", 255, SqlDbType.VarChar, true);
        public DBData Password = new DBData("Password", 255, SqlDbType.VarChar, true);

    }
ApplicantData applicant = new ApplicantData();

its working as

if (Request.Form[applicant.InvestorSWSId.Key] != null) { applicant.InvestorSWSId.Value = Request.Form[applicant.InvestorSWSId.Key]; } else { if (applicant.InvestorSWSId.IsRequired) { httpStatusCode = HttpStatusCode.BadRequest; apiResponse.ResponseCode = 102; apiResponse.ResponseStatus = false; apiResponse.ResponseMessage = "InvestorSWSId Required"; goto Output; } else { } }

but i want to use it as

foreach (var item in applicant)
                    {
                        
                        if (Request.Form[item.ID.Key] != null) { item.Value = Request.Form[item.Key]; } else { if (item.IsRequired) { httpStatusCode = HttpStatusCode.BadRequest; apiResponse.ResponseCode = 102; apiResponse.ResponseStatus = false; apiResponse.ResponseMessage = "InvestorSWSId Required"; goto Output; } else { } }
                    }

      }

how to use in foreach? please help to solve this issue please help me to fix this issue to use this with forech

  • Aside from the duplicate regarding iterating through class properties (which is specifically what you're asking), I would instead recommend re-structuring your model to *be iterable* in the way that you want. For example, a property that is an `IEnumerable` on that model could then be iterated. Or perhaps `ApplicantData` itself can implement `IEnumerable` or some similar interface to allow itself to be enumerated over. – David Aug 18 '23 at 12:44

0 Answers0