【发布时间】:2017-03-28 11:43:29
【问题描述】:
我有一个错误。这是
传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.Survey_D8226C5F5E348399740EDE08FDF0A956BDAD893915272C075AF983B0C50DA25E”, 但是这本词典需要一个“SurveySpaceProject.Models.User”类型的模型项。
我正在从数据库中进行调查,但它需要用户。我尝试了 Stackoverflow 的一些解决方案,但没有办法。你能帮我吗?这不是家庭作业。这是我的项目,我在 1 周前开始使用 asp.net。
My View
public ActionResult FillSurvey(int id)
{
var s = SSPEntity.getDB().Surveys.FirstOrDefault(x => x.ID == id);
return View(s);
}
我的 FillSurvey 页面:
@model SurveySpaceProject.Models.Survey
@{
ViewBag.Title = "FillSurvey";
}
<h2>FillSurvey</h2>
<div>
<h4>Survey</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.User.UserName)
</dt>
<dd>
@Html.DisplayFor(model => model.User.UserName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.IsDeleted)
</dt>
<dd>
@Html.DisplayFor(model => model.IsDeleted)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
还有我的调查模型
namespace SurveySpaceProject.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Survey
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Survey()
{
SurveyAnswers = new HashSet<SurveyAnswer>();
SurveyQuestions = new HashSet<SurveyQuestion>();
//SurveyUsers = new HashSet<SurveyUser>();
}
public int ID { get; set; }
public int UserID { get; set; }
[Required]
[StringLength(150)]
public string Title { get; set; }
[Column(TypeName = "date")]
public DateTime? CreatedDate { get; set; }
public bool IsDeleted { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyAnswer> SurveyAnswers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyQuestion> SurveyQuestions { get; set; }
public virtual User User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyUser> SurveyUsers { get; set; }
}
}
【问题讨论】:
-
你在哪里得到错误?究竟在哪部分代码?
-
您发布的视图使用
Survey作为其模型。获得此特定异常的唯一方法是,如果实际使用的视图具有User作为其模型。换句话说,您要么没有加载您认为的视图,要么您没有发布重要的实际视图的代码。 -
This Q/A 解释了错误,但您没有向我们显示正确的代码(您显示的内容不会产生该错误)。该错误清楚地表明您调用了具有
@model SurveySpaceProject.Models.User的视图(而不是您所显示的Survey) -
如果我通过了调查以外的任何内容,错误状态我需要调用“调查”。但是,如果我将调查作为模型传递,它会说明该错误。
标签: c# asp.net-mvc asp.net-mvc-3