【问题标题】:model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type传入字典的模型项的类型为 'System.Collections.Generic.List,但此字典需要类型的模型项
【发布时间】:2016-05-05 19:08:23
【问题描述】:

我有一个 ASP.NET MVC 5 应用程序,用户可以在其中查看项目索引。我添加了另一个名为“Applications”的数据库表,它将存储用户在从索引中申请项目时创建的应用程序对象。 应用模型:

public class Application
{
    public int ApplicationId { get; set; }
    public int ProjectId { get; set; }
    public string UserId { get; set; }
    public string CoverLetter { get; set; }

}

ProjectId 和 UserId 是外键(分别来自 dbo.Projects 和 dbo.AspNetUsers)

现在,对于 Projects 索引视图,我已将视图模型更改为 ApplicationTwoViewModel:

 public class ApplicationTwoViewModel
    {
        public IEnumerable<Project> Model1 { get; set; }
        public Application Model2 { get; set; }
    }

Index.cshtml:

@using Leepio.Models
@using Microsoft.AspNet.Identity
@model ApplicationTwoViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create" ,"Projects")
</p>
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Title", "Index", "Projects", new {SortOrder = (ViewBag.SortOrder==null?"Asc":(ViewBag.SortOrder=="Asc"?"Desc":"Asc")), SortBy = "Title"})

        </th>
        <th>
            @Html.ActionLink("Application Deadline", "Index", "Projects", new {SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "ApplicationDeadline"}) <br/>
            @Html.ActionLink("Hourly Rate (DKK)", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "HourlyRate" })
        </th>
       <th>
            @Html.ActionLink("Skill requirements", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "RequiredSkills" })
        </th>


    </tr>

@foreach (var item in Model.Model1) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ApplicationDeadline)<br/>
            @Html.DisplayFor(modelItem => item.HourlyRate)
        </td>
       <td>
            @Html.DisplayFor(modelItem => item.RequiredSkills)
        </td>


        <td>

            @if(item.UserId == User.Identity.GetUserId()) 

            {
                @Html.ActionLink("Edit", "Edit", "Projects", new {id = item.ProjectId})

                @Html.ActionLink("Delete", "Delete", "Projects", new {id = item.ProjectId}) 
            }

            @Html.ActionLink("Details", "Details", "Projects", new {id = item.ProjectId}) |
            @Html.ActionLink("Apply", "Index", "Applications", new { id = item.ProjectId }) |
        </td>
    </tr>
}

</table>
@{
    double TotalPage = @ViewBag.TotalPages;
}

<ul class="pagination">
    @for (int i = 1; i <= TotalPage; i++)
    {

        if (i == ViewBag.Page)
        {
            <li class="active"> @Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })</li>
        }
        else
        {
            <li>
                @Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })
            </li>
        }

    }
</ul>

当然我使用的是 Model.Model1 和 Model.Model2。

当尝试打开项目索引视图时,我得到以下信息: 传入字典的模型项的类型为“System.Collections.Generic.List`1[Leepio.Models.Project]”,但此字典需要“Leepio.Models.ApplicationTwoViewModel”类型的模型项。

项目控制器:

 public ActionResult Index(string SortOrder, string SortBy, string Page)
        {
            ViewBag.SortOrder = SortOrder;
            ViewBag.SortBy = SortBy;
            var projects = db.Projects.ToList();
            var model = new ApplicationTwoViewModel
            {
                Model1 = new List<Project>(projects),
                Model2 = new Application
                {

                    UserId = User.Identity.GetUserId(),
                    ProjectId = 11,
                    CoverLetter = "asdf",
                    ApplicationId = 23,
                }
            };

            //bunch of code I cut out regarding the sorting



        ViewBag.TotalPages =     Math.Ceiling(db.Projects.ToList().Count()/10.0);
        int page = int.Parse(Page == null ? "1" : Page);
        ViewBag.Page = page;
        projects = projects.Skip((page - 1) * 10).Take(10).ToList();
        return View(projects);

    }

我尝试过硬编码初始化 Model2,即使我不希望这样。什么可以解决这个错误?我感觉初始化已关闭,但我不确定

【问题讨论】:

  • 这将被否决作为答案,因此作为评论:try return View(model);
  • 您将错误的模型返回给您查看 (return View(projects);)。您需要将 var model = new ApplicationTwoViewModel 返回到您的视图 (return View(model);)
  • 事实上,这很简单,我称之为错字......
  • 实际上它确实修复了错误,谢谢:) 有时,当您搜索一个小时时,其他人可以立即看到问题。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 dictionary


【解决方案1】:

您的视图希望您接受 ApplicationTwoViewModel 类的实例,如您的 Index.cshtml 视图顶部所示:

@model ApplicationTwoViewModel

所以您只需要创建一个并将其传入,这应该很容易,因为您已经在 Controller Action 中构建了它:

// Pass the model to your View
return View(model);

由于您在初始化后显式编辑它,您可能希望在调用 return 之前执行以下操作,并确保对模型中的集合执行任何其他操作(即排序和过滤)还有:

// Perform your paging at the model level
model.Model1 = model.Model1.Skip((page - 1) * 10).Take(10).ToList();
// Now return it
return View(model);

【讨论】:

  • @crystyxn fyi...您应该接受这个答案是正确的,以便将来可以帮助其他人
猜你喜欢
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2013-10-17
  • 2015-09-27
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多