传递到字典中的模型项在ASP.net MVC中的类型为"System.Collections.Generic.List…" [英] The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC
问题描述
我正在尝试创建投票系统,但存在一些问题.我必须通过ADO.NET实体框架进行类生成其次,我创建存储库类以从我的控制器类中使用它我在PollController类中创建了ActionResult方法,该方法从我的数据库返回了所有池问题".
I'm trying to Create Poll System but there some issue. I have to classes generate it by ADO.NET Entity Framework Second I create Repository class to work with it from my controller class I create ActionResult Method in my PollController class that return All the Pool Questions from my DataBase.
我创建了一个粒子视图以显示特定问题的选项当我将此粒子放入Index.cshtml时,它会给我这个错误传递到字典中的模型项的类型为'System.Collections.Generic.List
I have one Particle View I create it to show the option for specific question When I put this particle in my Index.cshtml it give me this error The model item passed into the dictionary is of type 'System.Collections.Generic.List
这是我的代码 Poll.cs
public partial class Poll
{
public Poll()
{
this.PollOptions = new HashSet<PollOption>();
}
public int PollID { get; set; }
public Nullable<System.DateTime> AddedDate { get; set; }
public string AddedBy { get; set; }
public string QuestionText { get; set; }
public bool IsCurrent { get; set; }
public bool IsArchived { get; set; }
public virtual ICollection<PollOption> PollOptions { get; set; }
}
PollOption.cs
public partial class PollOption
{
public int OptionID { get; set; }
public Nullable<System.DateTime> AddedDate { get; set; }
public string AddedBy { get; set; }
public string OptionText { get; set; }
public int PollID { get; set; }
public Nullable<int> Votes { get; set; }
public virtual Poll Poll { get; set; }
}
PollRepository.cs
public class PollRepository
{
private PollPlatFormEntities entities = new PollPlatFormEntities();
public IQueryable<Poll> GetPolls()
{
return entities.Polls;
}
public Poll GetPoll(int Id)
{
return entities.Polls.FirstOrDefault(p => p.PollID == Id);
}
public IQueryable<Poll> CurrentPoll()
{
return entities.Polls.Where(c => c.IsCurrent == true);
}
public PollOption GetPollOption(int Id)
{
return entities.PollOptions.FirstOrDefault(o => o.OptionID == Id);
}
PollController.cs
public class PollsController : Controller
{
PollRepository pollRepository = new PollRepository();
//
// GET: /Polls/
public ActionResult Index()
{
var polls = pollRepository.GetPolls().ToList();
return View(polls);
}
}
这是我的index.cshtml视图
@model IEnumerable<PollSystem.Models.Poll>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.QuestionText)
@Html.Partial("PollItem")
</td>
</tr>
}
最后是我的粒子视图"
@model PollSystem.Models.Poll
<div id="poll-@Model.PollID" class="poll">
<h2>@Model.QuestionText</h2>
@using (Html.BeginForm(FormMethod.Post))
{
<ul class="poll-options">
@foreach (var option in Model.PollOptions)
{
<li class="option" id="option-@option.OptionID">
<input type="radio" id="option-@option.OptionID" value="@option.OptionID"/>
<label class="text" for="option-@option.OptionID">
@option.OptionText
</label>
</li>
}
</ul>
<button type="submit" name="poll-submit">Vote</button>
}
</div>
推荐答案
如果未将模型明确传递给局部视图,它将使用父视图的模型,从该父视图中调用局部视图.在您的情况下,您没有将任何模型传递给局部视图方法调用,因此它使用的父页面模型是 IEnumerable< PollSystem.Models.Poll>
作为局部模型的模型
If you do not explicitly pass a model to the partial view, it will use the model of the parent view, from which the partial view is called. In your case, you are not passing any model to the partial view method call, hence it is using the model of the parent page which is IEnumerable<PollSystem.Models.Poll>
as the model of the partial.
使用此重载的 Partial
方法,该方法将部分视图的模型作为第二个参数
Use this overload of Partial
method which takes the model of the partial view as the second parameter
public static MvcHtmlString Partial(
this HtmlHelper htmlHelper,
string partialViewName,
object model
)
因此,在主视图中,您可以将 item
变量传递给Partial调用
So in your main view, you can pass the item
variable to the Partial call
@model IEnumerable<PollSystem.Models.Poll>
@foreach (var item in Model) {
<tr>
<td>
@Html.Partial("PollItem",item)
</td>
</tr>
}
这篇关于传递到字典中的模型项在ASP.net MVC中的类型为"System.Collections.Generic.List…"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!