详细信息视图中的用户代码未对NullReferenceException进行处理

时间:2014-08-16 15:24:13

标签: asp.net-mvc

我在详细信息视图中收到错误:

App_Web_qv5v5vg0.dll中发生了'System.NullReferenceException'类型的异常但未在用户代码中处理

在@foreach(Model.Enrollments中的var项目)

中出现了错误

详细信息视图:

    @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>

    @using (Html.BeginForm("Comment", "Student", FormMethod.Post))
    {
        <div class="form-horizontal">
            <h4>Comments</h4>
            <hr />
            @Html.ValidationSummary(true)

            <input type="hidden" value="@Model.Student.ID" name="studentId" />

            <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.TextAreaFor(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="Add Comment" 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>

控制器:

public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Student studentfind = _db.Students.Find(id);
        Student student = _db.Students.Include(m => m.Comments).Include(m => m.Enrollments).SingleOrDefault(x => x.ID == id);
        if (student == null)
        {
            return HttpNotFound();
        }

        var enrollments = new Enrollment () { };

        var model = new DetailsViewModel
        {
            Comment = new Comment(),
            Student = student,
            Comments = student.Comments.OrderBy(c => c.Id).ToList()
        };

        return View(model);

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试迭代可能甚至不存在或尚未填充的属性。也许你打算迭代Model.Student.Enrollments?像这样:

@foreach (var item in Model.Student.Enrollments)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Course.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Grade)
        </td>
    </tr>
}