Shoaib Ahmed

Shoaib Ahmed

  • NA
  • 189
  • 5.4k

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.
  1. List<QuestionAnswer> QuestionAnswerList = new List<QuestionAnswer>();  
  2. foreach (JObject parsedObject in parsedArray.Children<JObject>())  
  3. {  
  4. foreach (JProperty parsedProperty in parsedObject.Properties())  
  5. {  
  6. string Question = parsedProperty.Name.ToString();  
  7. //string Answer = Convert.ToString(parsedProperty.Value);  
  8. if (Question.Contains("/"))  
  9. {  
  10. string[] Notes = Question.Split('/');  
  11. QuestionAnswer aNoteQuestionAnswer = new QuestionAnswer();  
  12. aNoteQuestionAnswer.Note = Notes[0].ToString();  
  13. aNoteQuestionAnswer.Question = Notes[1];  
  14. aNoteQuestionAnswer.Answer = Convert.ToString(parsedProperty.Value);  
  15. QuestionAnswerList.Add(aNoteQuestionAnswer);  
  16. }  
  17. else  
  18. {  
  19. QuestionAnswer aQuestionAnswer = new QuestionAnswer();  
  20. aQuestionAnswer.Question = Question;  
  21. aQuestionAnswer.Answer = Convert.ToString(parsedProperty.Value);  
  22. QuestionAnswerList.Add(aQuestionAnswer);  
  23. }  
  24. }  
  25. }  
  26. return View(QuestionAnswerList);  
  27. }  
QuestionAnswer Class :
  1. public class QuestionAnswer  
  2. {  
  3. public string Note { getset; }  
  4. public string Question { getset; }  
  5. public string Answer { getset; }  
  6. }  
My View :
  1. @model IEnumerable<EnracProject.Models.QuestionAnswer>  
  2. <table class="table table-bordered">  
  3. @foreach (var QuestionAnswers in Model)  
  4. {  
  5. <tr>  
  6. <td>@QuestionAnswers.Serial</td>  
  7. </tr>  
  8. <tr>  
  9. <td>@QuestionAnswers.Note</td>  
  10. </tr>  
  11. <tr>  
  12. <td>Question : @QuestionAnswers.Question</td>  
  13. <td>Answer : @QuestionAnswers.Answer</td>  
  14. </tr>  
  15. }  
  16. </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

Answers (1)