我不知道如何解决这个问题,出了问题,请帮忙!
问题是:
传递到字典中的模型项的类型为“System.Data.Entity.DynamicProxies.Student_0C394DD28745C7061A0EB9964A9FAD1A77E5EB1B24F61D61E2229472BCC850EE”,但此字典需要类型为“ContosoUniversity.ViewModels.DetailsViewModel”的模型项。
StudentController:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = _db.Students.Include(m => m.Comments).SingleOrDefault(x => x.ID == id);
if (student == null)
{
return HttpNotFound();
}
var model = new DetailsViewModel
{
Comment = new Comment(),
Student = student,
Comments = student.Comments.OrderBy(c => c.Id).ToList()
};
return View(student);
}
查看:
@model ContosoUniversity.ViewModels.DetailsViewModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Student.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.FirstMidName)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.FirstMidName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.EnrollmentDate)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.EnrollmentDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.Enrollments)
</dt>
<dd>
<table class="table">
<tr>
<th>Course Title</th>
<th>Grade</th>
</tr>
@foreach (var item in Model.Enrollments)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Course.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Grade)
</td>
</tr>
}
</table>
</dd>
</dl>
<div class="form-horizontal">
<h4>Movie</h4>
<hr />
@Html.ValidationSummary(true)
<input type="hidden" value="@Model.Comment.Id" name="commentId" />
<div class="form-group">
@Html.LabelFor(model => model.Comment.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment.Name)
@Html.ValidationMessageFor(model => model.Comment.Name)
</div>
</div>
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Comment.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment.Email)
@Html.ValidationMessageFor(model => model.Comment.Email)
</div>
</div>
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Comment.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment.Text)
@Html.ValidationMessageFor(model => model.Comment.Text)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div id="comments">
@foreach (var c in Model.Comments)
{
@Html.Partial("_Comment", c)
}
</div>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Student.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
详细信息 View 模型:
public Student Student { get; set; }
public List<Comment> Comments { get; set; }
public Comment Comment { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
评论模型:
public int Id { get; set; }
public int CommentId { get; set; }
public Student Student { get; set; }
[Required]
public string Name { get; set; }
[EmailAddress]
public string Email { get; set; }
[Required]
public string Text { get; set; }
学生模型:
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public string FullName
{
get
{
return string.Join(" ", FirstMidName, LastName);
}
}
public virtual ICollection<Enrollment> Enrollments { get; set; }
public List<Comment> Comments { get; set; }
如果有人不明白,我会尝试在“详细信息”页面上添加带有部分 View 的评论列表。
最佳答案
您正在尝试将 Student 对象传递到定义了它将处理的对象的 View ,即:@model ContosoUniversity.ViewModels.DetailsViewModel
您需要在 Controller 中向 View 传递一个DetailsViewModel,或者您必须重新考虑您正在做什么/需要做什么,并让 View 句柄对象为Student。
我不知道这个模型在哪里,但类似于:
@model ContosoUniversity.Models.Student
关于asp.net-mvc-4 - 详细信息 View 显示“System.Data.Entity.DynamicProxies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296766/