2016-01-29 96 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" 

     } 
}; 
相關問題