Shoaib Ahmed

Shoaib Ahmed

  • NA
  • 189
  • 4.9k

How to send List of objects which is in User defined class

Jan 8 2019 11:20 PM
My main Class 
public class QuestionAnswer
{
public string Question { get; set; }
public string Answer { get; set; }
public int Serial { get; set; }
public int SubmissionSerial { get; set; }
public List NoteTypeQuestions { get; set; }
}
In the main class, I am initializing List of another user-defined class(NoteTypeQuestion).
secondary class
public class NoteTypeQuestion
{
public string Note { get; set; }
public string NoteQuestion { get; set; }
public string NoteAnswer { get; set; }
}
In Controller, I am getting some data using api call and in that data if question contains "/" I want to send list of note type question with normal questions else I want to send only normal question answers to the View. 
Controller Method 
if (Question.Contains("/"))
{
string[] Notes = Question.Split('/');
string note = Notes[0].ToString();
string noteQuestion = Notes[1];
string noteAnswer = Convert.ToString(parsedProperty.Value);
aQuestionAnswer.NoteTypeQuestions.Add(new NoteTypeQuestion() { Note = note, NoteQuestion = noteQuestion, NoteAnswer = noteAnswer });
QuestionAnswerList.Add(aQuestionAnswer);
}
string Answer = Convert.ToString(parsedProperty.Value);


aQuestionAnswer.Question = Question;

aQuestionAnswer.Answer = Answer;

aQuestionAnswer.Serial = i;

QuestionAnswerList.Add(aQuestionAnswer);

return to the view 
return View(QuestionAnswerList);

In view 
@using EnracWebApp.Models
@model IEnumerable

I want to show List of QuestionAnswer data using foreach loop.
@foreach (var QuestionAnswers in Model)
{
@QuestionAnswers.Serial
@QuestionAnswers.Question
@QuestionAnswers.Answer
@foreach (var questionans in QuestionAnswers.NoteTypeQuestions)
{
@questionans.Note
@questionans.NoteQuestion
@questionans.NoteAnswer
}
}

The main problem is :
aQuestionAnswer.NoteTypeQuestions.Add(new NoteTypeQuestion() { Note = note, NoteQuestion = noteQuestion, NoteAnswer = noteAnswer });
How can I bind the data to the List ?

Answers (2)