2016-01-29 226 views
0

我试图硬编码一些数据进行测试,但似乎无法得到此正常工作。我确信我缺少一些简单的东西。不能隐式转换类型为System.Collections.Generic.List

这里是我的代码:

public async Task<ActionResult> GetClusterAnswers(int clusterId, int sectionId) 
{ 
    contractorId = UserInfo.targetCompanyID; 
    var questions = await CommonClient.GetGeneralQandAsBySection(sectionId, contractorId); 
    var selectedQuestion = questions.FirstOrDefault(q => q.QuestionClusterID == clusterId); 
    int? questionid = selectedQuestion.QuestionID; 

    QuestionsWithPairedAnswers question = new QuestionsWithPairedAnswers(); 
    question.QuestionID = questionid; 
    question.vchQuestionText = selectedQuestion.vchQuestionText; 
    question.vchTextElementOneHeader = selectedQuestion.vchTextElementOneHeader; 
    question.vchTextElementTwoHeader = selectedQuestion.vchTextElementTwoHeader; 

    question.Answers = new PairedAnswerTypes() 
    { 
     QuestionID = question.QuestionID, 
     PairedTextElementAnswerID = 1, 
     ContractorID = contractorId, 
     vchTextElementOne = "ABC", 
     vchTextElementTwo = "School Teachers" 
    }; 
    return Json(question, JsonRequestBehavior.AllowGet); 
} 

这里是我的模型:

public class QuestionsWithPairedAnswers 
{ 
    [Key] 
    public int? QuestionID { get; set; } 
    public string vchQuestionText { get; set; } 
    public string vchTextElementOneHeader { get; set; } 
    public string vchTextElementTwoHeader { get; set; } 
    public List<PairedAnswerTypes> Answers { get; set; } 
} 

public class PairedAnswerTypes 
{ 
    public int PairedTextElementAnswerID { get; set; } 
    public int? QuestionID { get; set; } 
    public int ContractorID { get; set; } 
    public string vchTextElementOne { get; set; } 
    public string vchTextElementTwo { get; set; } 
    public virtual QuestionsWithPairedAnswers Question { get; set; } 
} 

任何帮助是非常感谢!

+0

你会在哪一行发生错误。 –

回答

0

AnswersQuestionsWithPairedAnswers内的财产是List<PairedAnswerTypes>但在以下行中:

question.Answers = new PairedAnswerTypes() 

您尝试将其设置为PairedAnswerTypes

将其更改为:

question.Answers = new List<PairedAnswerTypes> 
{ 
    new PairedAnswerTypes() 
    { 
     QuestionID = question.QuestionID, 
     PairedTextElementAnswerID = 1, 
     ContractorID = contractorId, 
     vchTextElementOne = "ABC", 
     vchTextElementTwo = "School Teachers" 
    } 
} 

这样,你把新的答案内一个新的List所要求的性能。

+0

非常感谢! –

2

的问题是在这条线:

question.Answers = new PairedAnswerTypes() 
     { 
      QuestionID = question.QuestionID, 
      PairedTextElementAnswerID = 1, 
      ContractorID = contractorId, 
      vchTextElementOne = "ABC", 
      vchTextElementTwo = "School Teachers" 

     }; 

question.Answers是PairedAnswerTypes一个List你分配一个单独的PairedAnswerTypes,可以将此分配更改为列表initializiation和分配:

question.Answers = new List<PairedAnswerTypes> { 
    new PairedAnswerTypes() 
     { 
      QuestionID = question.QuestionID, 
      PairedTextElementAnswerID = 1, 
      ContractorID = contractorId, 
      vchTextElementOne = "ABC", 
      vchTextElementTwo = "School Teachers" 

     } 
}; 
相关问题