I am working project where I need to build ASP.net control based on JSON data. I am using the method below GetMeetingPollingQuestion to create this model.
Any suggestions/comments are greatly appreciated to improve the code further. Thanks.
JSON
[{"MeetingPollingQuestionId":1,"MeetingPollingQuestionType":"LongAnswerText","MeetingPollingId":1,"SequenceOrder":1,"MeetingPollingParts":[{"MeetingPollingPartsId":1,"Type":"Question","MeetingPollingQuestionId":1,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":1,"Type":"label","QuestionValue":"Do you have additional comments or concerns with these changes to the Guidelines?","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]}]},{"MeetingPollingQuestionId":12,"MeetingPollingQuestionType":"MultipleChoice","MeetingPollingId":1,"SequenceOrder":2,"MeetingPollingParts":[{"MeetingPollingPartsId":35,"Type":"Question","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":63,"Type":"label","QuestionValue":"Do you approve the following statement to be added for all current rituximab indications in the Guidelines: “An FDA-approved biosimilar is an appropriate substitute for rituximab”? ","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]},{"MeetingPollingPartsId":36,"Type":"Image","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":64,"Type":"FileManagerId","QuestionValue":null,"FileManagerId":14716,"FileName":"B-cell_1.2022_panel vote_Page_02 - Copy.png","FileData":"iVBORw.....","FileType":"image/png"}]},{"MeetingPollingPartsId":37,"Type":"Answers","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":65,"Type":"Answers","QuestionValue":"Yes","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null},{"MeetingPollingPartsValuesId":66,"Type":"Answers","QuestionValue":"No","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null},{"MeetingPollingPartsValuesId":67,"Type":"Answers","QuestionValue":"Abstain","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]}]}]
Method
public IEnumerable<MeetingPollingQuestionViewModel> GetMeetingPollingQuestion()
{
List<MeetingPollingQuestionViewModel> vm = new List<MeetingPollingQuestionViewModel>();
//JSON Data parse into class Model and set to object ListofMeetingPollingQuestion
foreach (MeetingPollingQuestion MeetingPollingQuestion in ListofMeetingPollingQuestion)
{
int SequenceOrder = MeetingPollingQuestion.SequenceOrder;
switch (MeetingPollingQuestion.MeetingPollingQuestionType)
{
case "LongAnswerText":
MeetingPollingQuestionViewModel LongAnswerText = new MeetingPollingQuestionViewModel();
LongAnswerText.QuestionType = "LongAnswerText";
var MeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;
var LongAnswerQuestion = MeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
var labelControl = LongAnswerQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
LongAnswerText.labelControl = $"{SequenceOrder}. {labelControl.QuestionValue}'";
LongAnswerText.textboxControl = $"textboxfor_{labelControl.MeetingPollingPartsValuesId}";
vm.Add(LongAnswerText);
break;
case "MultipleChoice":
MeetingPollingQuestionViewModel MultipleChoice = new MeetingPollingQuestionViewModel();
MultipleChoice.QuestionType = "MultipleChoice";
var MultipleChoiceMeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;
var MultipleChoiceQuestion = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
var MultipleChoicelabelControl = MultipleChoiceQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
MultipleChoice.labelControl = $"{SequenceOrder}. {MultipleChoicelabelControl.QuestionValue}'";
var MultipleChoiceImage = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Image");
var MultipleChoiceImageControl = MultipleChoiceImage.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "FileManagerId");
if (MultipleChoiceImageControl.FileManagerId != 0){
MultipleChoice.imageSRC = MultipleChoiceImageControl.FileData;
}
var MultipleChoiceAnswers = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Answers");
var MultipleChoiceAnswersControl = MultipleChoiceAnswers.MeetingPollingPartsValues.ToList();
List<CBRBControl> RadioButtonlist = new List<CBRBControl>();
foreach (var item in MultipleChoiceAnswersControl)
{
CBRBControl RadioButton = new CBRBControl();
RadioButton.Value = item.MeetingPollingPartsValuesId.ToString();
RadioButton.Label = item.QuestionValue;
RadioButtonlist.Add(RadioButton);
}
multipleChoice.RadioButtonName = $"radioList_{MultipleChoiceAnswers.MeetingPollingQuestionId}";
multipleChoice.RadioButtonList = RadioButtonlist;
vm.Add(MultipleChoice);
break;
}
}
return vm;
}
}
Model
public class MeetingPollingQuestionViewModel
{
public string QuestionType { get; set; }
public string labelControl { get; set; }
public string textboxControl { get; set; }
public string imageControl { get; set; }
public byte[] imageSRC { get; set; }
public string RadioButtonName { get; set; }
public List<CBRBControl> RadioButtonList { get; set; }
}
public class CBRBControl
{
public string Label { get; set; }
public string Value { get; set; }
}
class LongAnswerText : IMeetingPollingQuestion
This is certainly relevant to any answers here. \$\endgroup\$