C# 传入字典的模型项的类型为';System.Data.Entity.DynamicProxies.People';,但此词典需要类型为的模型项

C# 传入字典的模型项的类型为';System.Data.Entity.DynamicProxies.People';,但此词典需要类型为的模型项,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我需要在编辑页面中传递两个模型(因为我想用ViewModel构建一个MVC 4项目),但当我尝试在编辑页面中插入视图模型时,出现以下错误: 传递到字典中的模型项的类型为 “系统.数据.实体.动态验证.人员”-U E82A8FE6694DFF4D5ED1869045FE3E0A1855CC3C3CCA65B873F5F1556DABC2DC9F4, 但此词典需要类型为的模型项 “MVC_ViewModel.Models.ViewModel” 编辑页面(视图): @model MVC_ViewMod

我需要在编辑页面中传递两个模型(因为我想用ViewModel构建一个MVC 4项目),但当我尝试在编辑页面中插入视图模型时,出现以下错误:

传递到字典中的模型项的类型为 “系统.数据.实体.动态验证.人员”-U E82A8FE6694DFF4D5ED1869045FE3E0A1855CC3C3CCA65B873F5F1556DABC2DC9F4, 但此词典需要类型为的模型项 “MVC_ViewModel.Models.ViewModel”

编辑页面(视图):

@model MVC_ViewModel.Models.ViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>People</legend>

        @Html.HiddenFor(model => model.People.PeopleID)

        <div class="editor-label">
            @Html.LabelFor(model => model.People.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.People.Name)
            @Html.ValidationMessageFor(model => model.People.Name)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
@model IEnumerable<MVC_ViewModel.People>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PeopleID }) |
            @Html.ActionLink("Details", "Details", new { id=item.PeopleID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.PeopleID })
        </td>
    </tr>
}

</table>
namespace MVC_ViewModel.Models
{
    public class ViewModel
    {
        public Car Car { get; set; }
        public List<Car> Cars { get; set; }
        public People People { get; set; }
        public List<People> Peoples { get; set; }
        public Detail Detail { get; set; }
        public List<Detail> Details { get; set; }
    }
}
public ActionResult Index()
{
    return View(db.People.ToList());
}

...

public ActionResult Edit(int id = 0)
{
    People people = db.People.Find(id);
    if (people == null)
    {
        return HttpNotFound();
    }
    return View(people);
}

//
// POST: /CRUDViewModel/Edit/5

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(People people)
{
    if (ModelState.IsValid)
    {
        db.Entry(people).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(people);
}

如果有人能帮我的话,我需要理解错误。谢谢

您已在ASP.NET编辑页面中指定模型类型为
ViewModel

@model MVC_ViewModel.Models.ViewModel
然后向其传递一个
People
对象。您提供的代码中没有任何地方使用了
ViewModel

如果要将此更改为:

@model MVC_ViewModel.People
并将引用从
model.People.*
更改为
model.*
,则错误应消失

但是,我建议您应该返回一个
PeopleViewModel
,并将各种属性映射到该模型。去阅读一些博客、教程和如何最好地构建它的示例可能会很有用