Forums - C# Corner

Passing different lists of data to view from controller

Jan 11 2019 4:02 PM

First, I am getting data in parsedArray below,

JArray parsedArray = JArray.Parse(stringData);

I have a user defined type list named QuestionAnsweList. Now, For each parsedproperty I have initialized parsedproperty.name as my question and parsedproperty.value as my answer. I want to send Note with question answer whenever I find "/" in the question else send only question and answer in a list. Also, I want to send same Note question answer to be sent together so that I can print them together in view.

List<QuestionAnswer> QuestionAnswerList = new List<QuestionAnswer>();

foreach (JObject parsedObject in parsedArray.Children<JObject>())

{

foreach (JProperty parsedProperty in parsedObject.Properties())

{

string Question = parsedProperty.Name.ToString();

//string Answer = Convert.ToString(parsedProperty.Value);

if (Question.Contains("/"))

{

string[] Notes = Question.Split('/');

QuestionAnswer aNoteQuestionAnswer = new QuestionAnswer();

aNoteQuestionAnswer.Note = Notes[0].ToString();

aNoteQuestionAnswer.Question = Notes[1];

aNoteQuestionAnswer.Answer = Convert.ToString(parsedProperty.Value);

QuestionAnswerList.Add(aNoteQuestionAnswer);

}

else

{

QuestionAnswer aQuestionAnswer = new QuestionAnswer();

aQuestionAnswer.Question = Question;

aQuestionAnswer.Answer = Convert.ToString(parsedProperty.Value);

QuestionAnswerList.Add(aQuestionAnswer);

}

}

}

return View(QuestionAnswerList);

}

QuestionAnswer Class :

publicclass QuestionAnswer

{

publicstring Note { get; set; }

publicstring Question { get; set; }

publicstring Answer { get; set; }

}

My View :

@model IEnumerable<EnracProject.Models.QuestionAnswer>

<table class="table table-bordered">

@foreach (var QuestionAnswers in Model)

{

<tr>

<td>@QuestionAnswers.Serial</td>

</tr>

<tr>

<td>@QuestionAnswers.Note</td>

</tr>

<tr>

<td>Question : @QuestionAnswers.Question</td>

<td>Answer : @QuestionAnswers.Answer</td>

</tr>

}

</table>

In the table, I want Question and Answers to print in a trow tag. but Whenever a "/" question found in the controller I want to bind all those questions and answers print them here like In a row note then question answer . There can be many questions answers under same name note.

How can I solve this problem? I have given my tried code of controller model and view