MVC ViewModel - 需要定义viewmodel

时间:2014-10-24 06:07:34

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller asp.net-mvc-viewmodel

我正在将一个xml格式的调查问卷存储到名为Questionnaire的字符串类型数据字段中。数据库字段是contactID和Questionannire。我在MVC应用程序中这样做。有人告诉我ViewModel应该是什么样的吗?

xml想要

<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>

1 个答案:

答案 0 :(得分:0)

您的viewmodel可能如下所示:

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;}
 }

接下来,您需要阅读XML并将其放入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}