Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-4model-view-controllerasp.net-mvc-viewmodel

MVC ViewModel - Need to define viewmodel


I am storing a questionnaire which is in xml format to a string type data field called Questionnaire. The database fields are contactID and Questionannire. I am doing this in an MVC application.Can someone tell me what should the ViewModel look like ?

The xml would like

<xml>
   <Question>What is your country of origin?/<Question>
   <Answer>United Kingdom </Answer> 
   <Question>What is your place of birth?</Question>
   <Answer>United States </Answer> 
</xml>

Solution

  • Your viewmodel could look like this:

    public class VM { public IEnumerable<QA> MyQuestionsAnswers {get;set; } }
    //then you need a QA class
    public class QA{
       public List<string> Questions {get;set;}
       public List<string> Answers {get;set;}
     }
    

    Next you need to read the XML and put it into a List of QA...

    XDocument document = XDocument.Load(@"XMLFile1.xml"); 
    
    var TheQuestions = (from br in document.Descendants("Questions") 
                       select br).ToList();  
    var TheAnswers = (from or in document.Descendants("Answers") 
                       select or).ToList(); 
    var myQA = new QA{Questions=TheQuestions, Answers=TheAnswers}