错误:传入字典的模型项的类型为......但此字典需要类型为...的模型项“

时间:2017-04-26 10:49:16

标签: asp.net-mvc

我对编程很新,我在尝试运行程序时遇到了一些问题。我继续得到这个错误:“传入字典的模型项是'DCMS.Models.Projects.Project'类型,但是这个字典需要一个'DCMS.ViewModels.ProjectDetailsViewModel'类型的模型项。”  这是我的控制器:

        public ActionResult GetProjectView(int id)
            {
                var projectDataAccess = new ProjectDataAccess();
                var project = projectDataAccess.GetProject(id);
                if (project == null) return PartialView("Error");
                return View("ProjectView", project);
            }

这是我的观点的一部分:

@model DCMS.ViewModels.ProjectListViewModel

@if (Model != null)
{
    foreach (var project in Model.ProjectList)
    {
        <div class="accordion-container">
            <h3 class="accordion-header">
                @Html.ActionLink(project.Client.ClientName + " / " + project.ProjectName + " / " + project.ProjectNum ?? "<null>", "GetProjectView", new {id = project.Id})<span class="right">@Html.ActionLink("View Details", "GetProjectView", new {id = project.Id})</span>
            </h3>
            <div class="accordion-body">
                <table class="accordion-details">
                    <tr class="accordion-evenrow">
                        <td class="accordion-header-column">
                            @Html.LabelFor(m => project.ProjectStatus)
                        </td>
                        <td class="accordion-value-column">
                            @project.ProjectStatus
                        </td>

这是ProjectView:

@model DCMS.ViewModels.ProjectDetailsViewModel
@{
    ViewBag.Title = "Project Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 id="breadcrumbs">@Html.ActionLink("Projects", "Index", "Project") > @(Model.Project.ProjectName ?? "<null>")/@Model.Project.ProjectNum </h2>
<div id="tabs">
    <ul id="projectTabs" style="display:none;">
        <li><a href="#projectdetails">Project Details</a></li>
        <li><a href="#pricing">Client Pricing</a></li>
        <li><a href="#vendor">Vendor Pricing</a></li>
        <li><a href="#status">Status</a></li>
    </ul>
    <h2 style="margin:1px; font-size:20px;">@Model.Project.ProjectName /@Model.Project.ProjectNum</h2>
    <div id="projectdetails">
        @Html.Partial("Partial/EditProject")
    </div>
    <div id="pricing">
        @Html.Partial("Partial/Pricing")
    </div>
    <div id="status">
        @Html.Partial("Partial/ProjectStatus")
    </div>
    <div id="vendor">
        @Html.Partial("Partial/VendorPricing")
    </div>
</div>


<script type="text/javascript">
    $(document).ready(function(){
        $("#projectTabs").show();
        $("#tabs").tabs();
    });

</script>

如何将Project类型的项目传递给ProjectDetailsViewModel类型的View? 我将不胜感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

作为错误状态,

  

传递到字典中的模型项是类型的   &#39; DCMS.Models.Projects.Project&#39;,但这本词典需要一个模型   项目类型&#39; DCMS.ViewModels.ProjectDetailsViewModel&#39;

ViewModel和传递的模型应该相同。

public ActionResult GetProjectView(int id)
{
      ProjectDetailsViewModel _projectModel = new ProjectDetailsViewModel();
      var projectDataAccess = new ProjectDataAccess();
      var project = projectDataAccess.GetProject(id);
       _projectModel.Project = project ;
       if (project == null) return PartialView("Error");
       return View("ProjectView", _projectModel );
 }