传入字典的模型项的类型为System.Collections.Generic.List,但此字典需要类型的模型项

时间:2016-05-05 19:08:23

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

我有一个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。

尝试打开Projects索引视图时,我得到以下内容: 传递到字典中的模型项的类型为&#39; System.Collections.Generic.List`1 [Leepio.Models.Project]&#39;,但此字典需要类型为&#39的模型项; Leepio.Models.ApplicationTwoViewModel&#39;

ProjectsController:

 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硬编码,即使我不想这样做。有什么可以解决这个错误?我有一种感觉,初始化已关闭,但我不确定

1 个答案:

答案 0 :(得分:3)

您的视图希望您接受ApplicationTwoViewModel课程的Index.cshtml课程,见@model ApplicationTwoViewModel 视图顶部:

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

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

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); 调用之前执行以下操作,并确保对其执行任何其他操作(即排序和过滤)您的模型中的集合:

m == n