温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Partial view crashes on non-existent data
asp.net-mvc c#

c# - 部分视图因不存在的数据而崩溃

发布于 2020-03-28 23:40:24

我正在尝试获取部分视图以显示一些数据。它嵌套在另一个非局部视图中,如下所示:

<div id="ProizvodPodaci" style="display:flex;justify-content:center;align-items:center;">
    @{Html.RenderAction("PrikazStanja", "Proizvod", new { Id = 0});}
</div>

我给它这样的数据:

<script type="text/javascript">
function pretrazi(ID) {
    var a = document.getElementById("proizvodID").value;
    if (a == "") {
        ID = 0;
    }

    if (ID == 0) {
        alert("Molimo unesite ID za pretragu.");
        return;
    }

    $.ajax({
        url: '@Url.Action("PrikazStanja", "Proizvod")',
        data: { id: ID},
        success: function (result) {
            $('#ProizvodPodaci').html(result);
        }
    });
}

这是局部视图本身。注意带有@ *和* @的“注释部分”。

@model IEnumerable<azilZaPse.Models.ProizvedeniProizvodiBO>

<table class="table">
    <caption>Rezultati pretrage za proizvod: @* @Model.FirstOrDefault().proizvod.IdProizvoda *@ </caption>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.proizvodjac.IdProizvodjaca)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.proizvodjac.NazivProizvodjaca)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.proizvodnja.DostupneKolicine)
        </th>
    </tr>

    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.proizvodjac.IdProizvodjaca)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.proizvodjac.NazivProizvodjaca)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.proizvodnja.DostupneKolicine)
        </td>
    </tr>
    }

    <tr>
        <td colspan="3">
            Trenutne količine u azilu su: @* @Model.FirstOrDefault().proizvod.Kolicina *@
        </td>
    </tr>
</table>

我基本上是为它提供一个IEnumerable的类列表(ProizvedeniProizvodiBO),该列表由其他3个类(Proizvodjac,Proizvodnja和Proizvod)组成。这些类通过linq被“填充”。

If I provide it with a "valid" set of data (aka some that actually exists in the database) all works as it should. If I provide it with non-valid data (like an ID that does not exist in the database) the two commented lines crash the entire thing. With them removed, all is as it should, the other lines work ok when given "invalid input" and just don't display anything (as they should).

You may notice that I'm trying to give it "first or default". I also tried "take(1)". Both proizvod.IdProizvoda and proizvod.Kolicina and generally the entire proizvod should be the exact same in all the rows (only the other 2 classes are different per row), but I only need it displayed once.

Basically is there a way to put in some default values for the 2 lines? Sorry if the question's a mess.

查看更多

查看更多

提问者
Drvo Foka
被浏览
128
daremachine 2020-01-31 17:58

您需要使用空条件运算符(?。)俗称“猫王运算符”。

@Model.FirstOrDefault()?.proizvod?.IdProizvoda 

从Microsoft Doc

在C#6或更高版本中可用,null运算符仅在操作数被评估为非null时才使用access或[]元素访问C#成员以对其操作数执行操作。否则,返回null。如果a的值为空,则为a的结果。.X还是a?[X]为空。如果a评估为非零,则a的结果?.X还是a?[X]分别与ax或[x]的结果相同。

对于默认值,您可以使用razor @(_ your_code_here)

对于

  1. 默认值可以使用colasence运算符

@(Model.FirstOrDefault()?.proizvod?.IdProizvoda ?? "my default value")

  1. 不同值的三元运算符?

@(Model.FirstOrDefault()?.proizvod?.IdProizvoda ? "yes" : "no")

希望对你有帮助