视图中缺少ViewData

时间:2019-01-05 22:08:53

标签: c# asp.net-core asp.net-core-mvc

在我的“乐团详细信息”网页上(采用以下URL结构:乐团/详细信息/ {id}),当我单击某个音乐家的“详细信息”链接时(应该将我带到该音乐家/详细信息/ {id}),我的音乐家详细信息视图文件中出现空引用异常。它被抛出在我的代码中的以下链接中,以创建工具,但我无法确定它不喜欢哪个变量:

<a asp-action="create" asp-controller="instrument" asp-route- 
id="@orchestra.Id" asp-route-musicianId="@Model.Id"> Create an 
instrument</a>

我已经在MusicianController中检查了Musician Details方法,我认为这可能是模型绑定问题。我也查看了“音乐家详细信息视图”,但不明白为什么会引发错误。

-MusicianController文件中的“音乐家详细信息”方法--------------------

public IActionResult Details([Bind(Prefix = "id")] int musicianId)
{
    var musician = _repo.ReadMusician(musicianId);
    if(musician == null)
    {
        return RedirectToAction("Details", "Orchestra", new { id = 
        musicianId});
    }

    ViewData["Musician"] = musician;
    return View(musician);
}

-音乐家详细信息查看文件---------------------------------

@using OrchestraManagement.DbFirstData
@model Musician

@{
    var orchestra = (Orchestra)ViewData["Orchestra"];
    //var musician = (Musician) ViewData["Musician"];
    ViewData["Title"] = "Details";
}

<h2>Details</h2>
<div>
    <input asp-for="Id" type="hidden" value="@Model.Id"/>
    @*<input name="orchestraId" value="@orchestra.Id" type="hidden"/>*@
    @*<input name="musicianId" value="@musician.Id" type="hidden"/>*@
    <h4>Musician</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Id)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Id)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.FirstName)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.LastName)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Section)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Section)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.SectionLeader)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.SectionLeader)
        </dd>
        <dt>
            Number of instruments:
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Instrument.Count)
        </dd>
    </dl>
    <hr/>
    <h1> Instruments For This Musician:</h1>
    <table class="table">
        <thead>
            <tr>
                <th>
                Serial Number
                </th>
                <th>
                Description
                </th>
                <th>
                Maintenance Date
                </th>
                <th>
                Condition
                </th>
                <th>
                Links:
                </th>
            </tr>
        </thead>
   </table>
   <tbody>
       @foreach(var item in Model.Instrument)
       {
        <tr>
            <td> @Html.DisplayFor(modelItem => item.SerialNumber) </td>
            <td> @Html.DisplayFor(modelItem => item.Description)</td>
            <td> @Html.DisplayFor(modelItem => item.MaintenanceDate)</td>
            <td> @Html.DisplayFor(modelItem => item.Condition))</td>
            <td>
                <a asp-action="edit" asp-controller="instrument" asp-route-id="@Model.Id" asp-route-instrumentId="@item.Id">Details</a>
                <a asp-action="delete" asp-controller="instrument" asp-route-id="@Model.Id" asp-route-instrumentId="@item.Id">Delete</a>
            </td>
        </tr>
       }
   </tbody>
   </table>
</div>

<div>
    <a asp-action="create" asp-controller="instrument" asp-route-id="@orchestra.Id" asp-route-musicianId="@Model.Id"> Create an instrument</a> |
    @*<a asp-action="Create" asp-controller="Musician" asp-route-id="@Model.Id"> Create a musician</a> |*@
    <a asp-action="edit" asp-controller="musician" asp-route-id="@Model.Id"> Edit this musician</a>|
    <a asp-action="details" asp-controller="orchestra" asp-route-id="@orchestra.Id">Back to Orchestra Details</a>
</div>

-我的数据库第一乐团存储库文件中的ReadMusician方法-------

public Musician ReadMusician(int musicianId)
{
    return _db.Musician.Include(m => m.Instrument).FirstOrDefault(m => m.Id 
    == musicianId);
}

1 个答案:

答案 0 :(得分:0)

在引发错误的视图中,访问@orchestra.Id

应该是从

设置的
 var orchestra = (Orchestra)ViewData["Orchestra"];

但是控制器操作未在Orchestra中设置ViewData,因此@orchestra很可能是null

在尝试访问null引用上的Id属性时,这将导致null引用异常。

建议您在返回视图之前在操作中设置所需的数据。

public IActionResult Details([Bind(Prefix = "id")] int musicianId) {
    var musician = _repo.ReadMusician(musicianId);
    if(musician == null) {
        return RedirectToAction("Details", "Orchestra", new { id = 
        musicianId});
    }

    Orchestra orchestra  = //...code to get the Orchestra

    ViewData["Orchestra"] = orchestra;
    return View(musician);
}