I am working project where I need to build ASP.net control base on JSON data. I am using this method below to get all the data into class. I as using FirstOrDefault
and the Where(x => x.Type == ???
to drill down into the data. I think the logic could be simplified, Any suggestions are welcomed. working sample below
JSON data
[
{
"MeetingPollingQuestionId": 2,
"MeetingPollingQuestionType": "LongAnswerText",
"MeetingPollingId": 3,
"SequenceOrder": 1,
"MeetingPollingParts": [
{
"MeetingPollingPartsId": 2,
"Type": "Question",
"MeetingPollingQuestionId": 2,
"MeetingPollingPartsValues": [
{
"Type": "label",
"QuestionValue": "This is a long question",
"FileManagerId": 0,
"FileName": null,
"FileData": null,
"FileType": null
}
]
}
]
},
{
"MeetingPollingQuestionId": 3,
"MeetingPollingQuestionType": "MultipleChoice",
"MeetingPollingId": 3,
"SequenceOrder": 2,
"MeetingPollingParts": [
{
"MeetingPollingPartsId": 3,
"Type": "Question",
"MeetingPollingQuestionId": 3,
"MeetingPollingPartsValues": [
{
"Type": "label",
"QuestionValue": "this is a multiple choice question",
"FileManagerId": 0,
"FileName": null,
"FileData": null,
"FileType": null
}
]
},
{
"MeetingPollingPartsId": 4,
"Type": "Image",
"MeetingPollingQuestionId": 3,
"MeetingPollingPartsValues": [
{
"Type": "Image",
"QuestionValue": null,
"FileManagerId": 14552,
"FileName": null,
"FileData": null,
"FileType": null
}
]
},
{
"MeetingPollingPartsId": 5,
"Type": "Answers",
"MeetingPollingQuestionId": 3,
"MeetingPollingPartsValues": [
{
"Type": "radio",
"QuestionValue": "Yes",
"FileManagerId": 0,
"FileName": null,
"FileData": null,
"FileType": null
},
{
"Type": "radio",
"QuestionValue": "No",
"FileManagerId": 0,
"FileName": null,
"FileData": null,
"FileType": null
},
{
"Type": "radio",
"QuestionValue": "Abstain",
"FileManagerId": 0,
"FileName": null,
"FileData": null,
"FileType": null
}
]
}
]
}
]
method creating controls
public class LongAnswerText : IMeetingPollingQuestion
{
private List<MeetingPollingParts> meetingPollingParts;
private string label = null;
private string textbox = null;
private string type = null;
public LongAnswerText(List<MeetingPollingParts> meetingPollingParts)
{
this.SequenceOrder = SequenceOrder;
this.MeetingPollingId = MeetingPollingId;
this.MeetingPollingQuestionId = MeetingPollingQuestionId;
this.meetingPollingParts = meetingPollingParts;
var MeetingPollingPartsValuesLabel = new List<MeetingPollingPartsValues>();
MeetingPollingPartsValuesLabel = meetingPollingParts.SelectMany(s => s.MeetingPollingPartsValues).ToList();
var labelControl = MeetingPollingPartsValuesLabel.Where(x=>x.Type=="label").FirstOrDefault();
var textboxControl = MeetingPollingPartsValuesLabel.Where(x => x.Type == "textbox").FirstOrDefault();
this.label = LabelControl(string.Format("label_{0}", labelControl.MeetingPollingPartsValuesId), labelControl.QuestionValue);
this.textbox = TextboxControl(string.Format("label_{0}", labelControl.MeetingPollingPartsValuesId));
}
public string LabelControl(string target, string text){
return string.Format("@Html.LabelFor('{0}'>{1}</input>",target,text);
}
public string TextboxControl(string target) {
return string.Format("@Html.TextBoxFor('{0}')",target);
}
}
Output View
<div class="form-group row">
div class="col-md-12">
@Html.LabelFor(model => model.label_1)
@Html.TextBoxFor(x => x.label_1, new { id = "label_1", @class = "form-control" })
</div>
</div>
fiddle
https://dotnetfiddle.net/j6YIPN
.Where(x=>x.Type=="label").FirstOrDefault();
=>.FirstOrDefault(x=>x.Type=="label");
\$\endgroup\$this.SequenceOrder = SequenceOrder;
? \$\endgroup\$