2017-03-07 18 views
-1

這裏是我的模型類本詞典需要類型的模型項目「System.Collections.Generic.IEnumerable

public class SurveyModel 
{ 
    public SurveyModel() 
    { 
     this.Name = string.Empty; 
     this.Url = string.Empty; 
    } 
    [Key] 
    public string Name { get; set; } 
    public int? ResponseCount { get; set; } 
    public string Url { get; set; } 

    public SurveyModel(SurveyMonkey.Containers.Survey survey) 
    { 
     Name = survey.Nickname; 
     ResponseCount = survey.ResponseCount; 
     Url = survey.AnalyzeUrl; 
    } 

我contrroller

public class SurveysController : Controller 
{ 
    private SurveyMonkeyApi _surveyMonkey; 

    public SurveysController(ISurveyMonkeyApiFactory factory) 
    { 
     // _apiFactory = factory.Create(); 
    } 
    public SurveysController() 
    { 

    } 

    // GET: Surveys 

    public ActionResult Index() 
    { 
     using (var api = new SurveyMonkeyApi("zgSdm04SEefy09ONaxV6b0z5rDOoRHXffGXMAasySfAyxUfCTN4x3AR9IyK5NVoRrBKB27bT-SMlbbL0dI2vUNYQiRNZqbslM0-KATC9JwWblgx4mieUwNxoDzC54lxe")) 
     { 
      IEnumerable<Survey> surveys = api.GetSurveyList(); 

      return View(surveys.ToList()); 
     } 
    } 
} 

我的觀點

@model IEnumerable<SimpleSurveyTest.Models.SurveyModel> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ResponseCount) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Url) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ResponseCount) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Url) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.Name }) | 
      @Html.ActionLink("Details", "Details", new { id=item.Name }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.Name }) 
     </td> 
    </tr> 
} 
</table> 

我請繼續收到此錯誤

傳遞到詞典中的模型項的類型爲「System.Collections.Generic.List`1 [SurveyMonkey.Containers.Survey]」

但本詞典需要類型的模型項'System.Collections.Generic.IEnumerable1[SimpleSurveyTest.Models.SurveyModel]'

+2

你傳入** **調查模型控制器,但鑑於你期待** ** SurveyModel。 –

+2

在視圖中,使用模型IEnumerable 或Survery實體嘗試替換模型IEnumerable

回答

0

在該方法中 ) // GET:調查

public ActionResult Index() 
{ 
    using (var api = new SurveyMonkeyApi("zgSdm04SEefy09ONaxV6b0z5rDOoRHXffGXMAasySfAyxUfCTN4x3AR9IyK5NVoRrBKB27bT-SMlbbL0dI2vUNYQiRNZqbslM0-KATC9JwWblgx4mieUwNxoDzC54lxe")) 
    { 
     IEnumerable<Survey> surveys = api.GetSurveyList(); 

     return View(surveys.ToList()); 
    } 
} 

交換

return View(surveys.ToList()); 

隨着

return View(surveys.Select(s => new SurveyModel(s))); 
相關問題