C# 此词典需要类型为';System.Collections.Generic.IEnumerable
这是我的模型课C# 此词典需要类型为';System.Collections.Generic.IEnumerable,c#,surveymonkey,C#,Surveymonkey,这是我的模型课 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 Ur
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;
}
我的控制者
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());
}
}
}
公共类调查控制器:控制器
{
私人调查公司;
公共调查控制员(iSurveyMonkeyaPi工厂)
{
//_apiFactory=factory.Create();
}
公共调查控制员()
{
}
//获取:调查
公共行动结果索引()
{
使用(var api=new SurveyMonkeyApi(“zgSdm04SEefy09ONaxV6b0z5rDOoRHXffGXMAasySfAyxUfCTN4x3AR9IyK5NVoRrBKB27bT-SMLBBL0DI2VunyQBSLM0-KATC9JwWblgx4mieUwNxoDzC54lxe”))
{
IEnumerable surveys=api.GetSurveyList();
返回视图(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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.ResponseCount)
@DisplayNameFor(model=>model.Url)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.ResponseCount)
@DisplayFor(modelItem=>item.Url)
@ActionLink(“编辑”,“编辑”,新的{id=item.Name})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.Name})|
@ActionLink(“删除”,“删除”,新的{id=item.Name})
}
我一直在犯这个错误
传递到字典的模型项的类型为“System.Collections.Generic.List`1[SurveyMonkey.Containers.Survey]”
但此词典需要一个类型为“System.Collections.Generic.IEnumerable1[SimpleSurveyTest.Models.SurveyModel]”的模型项,在该方法中为“”
)
//获取:调查
public ActionResult Index()
{
using (var api = new SurveyMonkeyApi("zgSdm04SEefy09ONaxV6b0z5rDOoRHXffGXMAasySfAyxUfCTN4x3AR9IyK5NVoRrBKB27bT-SMlbbL0dI2vUNYQiRNZqbslM0-KATC9JwWblgx4mieUwNxoDzC54lxe"))
{
IEnumerable<Survey> surveys = api.GetSurveyList();
return View(surveys.ToList());
}
}
与
您正在控制器中传递测量模型,但在视图中需要测量模型。请尝试在视图中用模型IEnumerable或测量实体替换模型IEnumerable
return View(surveys.ToList());
return View(surveys.Select(s => new SurveyModel(s)));