【问题标题】:Error on page: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies页面错误:传入字典的模型项的类型为“System.Data.Entity.DynamicProxies”
【发布时间】:2012-11-09 16:53:54
【问题描述】:

我在详细信息视图页面上收到此错误:

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.cpd_certificates_65AF30842281867E2F4F6A1026590271109C12A85C8175394F14EF6DE429CBC7', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[cpd.Models.cpd_certificates]'. 

这是我的控制器:

 public class Default8Controller : Controller
{
    //
    // GET: /Default8/
    private cpdDbContext db = new cpdDbContext();

    public ActionResult Index()
    {


        return View(db.CPD.ToList());
    }

    //
    // GET: /Default8/Details/5

    public ActionResult Details(int id)
    {

        cpd_certificates cpd_ = db.Cert.Find(id);

        return View(cpd_);
    }

以下是我的详细信息视图:

@model IEnumerable<cpd.Models.cpd_certificates>

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>
 <table>
        <tr>
            <th>
                certificateNo
        </th>
        <th>
            Mark
        </th>
          <th></th>
    </tr>
       @foreach (var item in Model) {
            <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CertificateNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mark)
        </td>

    </tr>
}

</table>

下面是 cpd_certificates 模型:

public class cpd_certificates
{

        [Key]
        public int CertificateNo { get; set; }
        public int QuizNo { get; set; }


        public DateTime? DateReceived { get; set; }
        public DateTime? DatePaid { get; set; }
        public string Mark { get; set; }
        public int? AccreditationNo { get; set; }


        public int? ID { get; set; }
        public virtual cpd_recipients Recipients { get; set; }

}

我有两个模型,我可以在我的索引上查看列表。我打算当我单击详细信息链接时,它会将我带到另一个模型/表格中获得的证书和考试的详细信息。因此,简而言之,两个模型在 CPD 上具有 PK ID,在 Cert 上具有 FK ID。索引页面只显示个人信息,这个页面很好。 单击详细信息时,它会显示其他模型/表格数据,即该人的许多证书。

【问题讨论】:

  • 你能显示cpd.Models.cpd_certificates模型吗
  • @mohit-arora 我已经编辑并提交了证书的模型。

标签: asp.net-mvc-3 entity-framework c#-4.0


【解决方案1】:

您应该将模型作为 IEnumerable 条目列表传递到视图中,而不是将其作为单个条目传递。 在您的控制器中,您希望显示一个条目的详细信息,但在视图中,希望显示所有条目的详细信息。首先您应该决定要显示的内容。 要显示一个条目的详细信息,请使用:

控制器:

public ActionResult Details(int id)
{
    cpd_certificates cpd_ = db.Cert.Find(id);
    return View(cpd_);
}

查看:

@model cpd.Models.cpd_certificates  
@{
ViewBag.Title = "Details";}

<h2>Details</h2>
 <table>
    <tr>
        <th>
            certificateNo
        </th>
        <th>
            Mark
        </th>
        <th></th>
    </tr>
    <tr>
        <td>
            @Html.DisplayFor(model => model.CertificateNo)
        </td>
        <td>
            @Html.DisplayFor(model => model.Mark)
        </td>

    </tr>
}

</table>

当你想列出所有条目时,使用这个:

控制器:

public ActionResult List()
{
    cpd_certificates cpd_ = db.Cert.ToList();
    return View(cpd_);
}

查看:

@model IEnumerable<cpd.Models.cpd_certificates>

@{
    ViewBag.Title = "List";
}

<h2>List</h2>
 <table>
        <tr>
            <th>
                certificateNo
        </th>
        <th>
            Mark
        </th>
          <th></th>
    </tr>
       @foreach (var item in Model) {
        <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CertificateNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mark)
        </td>

    </tr>
}

</table>

【讨论】:

  • 请问如何将其作为 IEnumerable 条目列表传递。
  • 这是我在这一行遇到的错误 db.Cert.ToList():无法隐式转换类型 'System.Collections.Generic.List' 到 'cpd.Models.cpd_certificates'
  • 不要将项目列表传递给需要一个特定项目的视图。它将被解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 2019-06-01
  • 2011-09-06
  • 1970-01-01
  • 2021-10-01
  • 2011-02-14
相关资源
最近更新 更多