详细信息视图正在显示“System.Data.Entity.DynamicProxies

时间:2014-08-13 21:49:03

标签: asp.net-mvc-4 razor

我不知道如何解决问题,有些不对劲,请帮助!

问题是:

传递到字典中的模型项的类型为'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>

DetailsViewModel:

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; }

如果有人不明白我正在尝试在详细信息页面添加评论列表,部分视图。

2 个答案:

答案 0 :(得分:1)

您正在尝试将学生对象传递到您定义它将处理的对象的视图,即:@model ContosoUniversity.ViewModels.DetailsViewModel

您需要在控制器中向视图传递DetailsViewModel,或者您必须重新考虑您正在做/需要做的事情,并使视图句柄对象成为学生。

我不知道这个模型在哪里,但是像:

@model ContosoUniversity.Models.Student 

答案 1 :(得分:1)

假设您的共享视图文件夹中有一个名为_Comment.cshtml的DisplayTemplates文件夹(即/Views/Shared/DisplayTemplates/_Comment.cshtml之类的路径),您应该尝试以下操作:

  1. 将_Comment.cshtml重命名为Comment.cshtml
  2. 替换以下内容:

    <div id="comments">
       @foreach (var c in Model.Comments)
       {
           @Html.Partial("_Comment", c)
       }
     </div>
    
  3. 以下内容:

    @Html.DisplayFor(model => model.Comments)
    

    我只是自己与之斗争,文件名中的下划线就是破坏事物。