【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC [duplicate]传递到字典中的模型项是 ASP.net MVC 中的“System.Collections.Generic.List ...”类型 [重复]
【发布时间】:2017-12-10 11:57:48
【问题描述】:

我正在尝试创建投票系统,但出现了一些问题。 我必须通过 ADO.NET Entity Framework 生成它 其次,我创建 Repository 类以从我的控制器类中使用它 我在 PollController 类中创建 ActionResult 方法,该方法从我的数据库返回所有池问题。

我有一个粒子视图我创建它来显示特定问题的选项 当我把这个粒子放在我的 Index.cshtml 中时,它给了我这个错误传入字典的模型项的类型是'System.Collections.Generic.List

这是我的代码 投票.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>

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    如果您没有显式地将模型传递给分部视图,它将使用父视图的模型,从该模型中调用分部视图。在您的情况下,您没有将任何模型传递给局部视图方法调用,因此它使用父页面的模型 IEnumerable&lt;PollSystem.Models.Poll&gt; 作为局部模型。

    使用Partial方法的重载,它以局部视图的模型作为第二个参数

    public static MvcHtmlString Partial(
        this HtmlHelper htmlHelper,
        string partialViewName,
        object model
    )
    

    所以在您的主视图中,您可以将 item 变量传递给 Partial 调用

    @model IEnumerable<PollSystem.Models.Poll>
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.Partial("PollItem",item) 
            </td>
        </tr>
    }
    

    【讨论】:

    • 谢谢..现在正在工作...我觉得你的解释更有用
    【解决方案2】:

    请尝试

     @model IEnumerable<PollSystem.Models.Poll>
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.QuestionText)
                @Html.Partial("PollItem",item) 
            </td>
        </tr>
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-21
      • 2019-01-12
      • 2014-01-02
      • 2018-07-24
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多