简体   繁体   English

部分视图在不存在的数据上崩溃

[英]Partial view crashes on non-existent data

I'm trying to get a partial view to display some data.我正在尝试获取部分视图以显示一些数据。 It is nestled in another non-partial view like this:它位于另一个非局部视图中,如下所示:

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

And I'm giving it data like this:我给它的数据是这样的:

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

Here is the partial view itself.这是局部视图本身。 Note the "commented out parts" with @* and *@.注意带有@* 和*@ 的“注释掉的部分”。

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

I'm basically providing it with an IEnumerable list of a class (ProizvedeniProizvodiBO) that consists of 3 other classes (Proizvodjac, Proizvodnja and Proizvod).我基本上为它提供了一个类 (ProizvedeniProizvodiBO) 的 IEnumerable 列表,该列表包含 3 个其他类(Proizvodjac、Proizvodnja 和 Proizvod)。 The classes are "filled" through linq.这些类是通过 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.如果我向它提供无效数据(例如数据库中不存在的 ID),则两条注释行会使整个内容崩溃。 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)".我也试过“采取(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. proizvod.IdProizvoda 和 proizvod.Kolicina 以及通常整个 proizvod 在所有行中都应该完全相同(只有其他 2 个类每行不同),但我只需要显示一次。

Basically is there a way to put in some default values for the 2 lines?基本上有没有办法为 2 行输入一些默认值? Sorry if the question's a mess.对不起,如果问题是一团糟。

You need use null conditional operator (?.) is colloquially referred to as the "Elvis operator".您需要使用空条件运算符(?.) 通俗地称为“猫王运算符”。

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

From microsoft doc来自微软文档

Available in C# 6 or later, a null operator uses an access, or., [] Element to access a C # member to perform an operation on its operand only if the operand is evaluated as other than null;在 C# 6 或更高版本中可用,空运算符使用访问或.、[] 元素访问 C# 成员以对其操作数执行操作,仅当操作数被评估为非空值时; otherwise, it returns null.否则,它返回 null。 It is If a evaluates to null, the result of a?如果 a 的计算结果为 null,则 a? .X or a? .X 还是 a? [X] is null. [X] 为空。 If a is evaluated to be non-zero, the result of a?如果 a 被评估为非零,a? .X or a? .X 还是 a? [X] is the same as the result of ax or a [x], respectively. [X] 分别与 ax 或 a [x] 的结果相同。

And for default value you can use razor @(_your_code_here) .对于默认值,您可以使用 razor @(_your_code_here)

For为了

  1. default value you can use colasence operator ??您可以使用colasence 运算符的默认值??

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

  1. different values ternary operator ?不同的值三元运算符? :

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

Hope it help希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM