使foreach循环中的值默认为某个值 [英] Make values in a foreach loop default to something

查看:92
本文介绍了使foreach循环中的值默认为某个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个foreach循环和它返回的模型的返回值为null ...我不想要去每个单独的行并说如果值为null则将其留空...我想知道是否有更快的解决方案。这是一个问题,我有多个区域...





这是我的foreach循环的样子...... br />

Okay so, I have a foreach loop and its returning values of the model it goes through as null... I dont want to have to go to each individual row and say if the value is null leave it blank... I was wondering if there is a faster solution to this. This is a problem that I am having multiple areas...


Here is what my foreach loop looks like....

@foreach (var item in Model)
{
    <tr>

        <td>
            <div class="btn-group btn-group-sm btn-group-solid">
                <a href="@Url.Action("OverviewDetails","PrototypeOverview",new { id= item.prototype_id})" class="btn btn-default"><i class="fa fa-eye"></i></a>
                <a href="#" class="btn btn-default"><i class="fa fa-file"></i></a>
            </div>
        </td>
        <td>@item.prototype_number</td>

        <td>@item.prototype_category.prototype_category_name</td>

        <td>@item.prototype_sub_category</td>

        <td>@item.season</td>
        <td>@item.garment_quantity</td>
        <td>@item.garment_size</td>
        <td>@item.for_reason</td>

        <td>
            @item.purpose.
        </td>

        <td>@item.fit</td>  @*is this correct*@
        <td>@item.style_construction_reference</td>
        <td>@item.approval_code</td>
        <td>@item.is_handsand</td>
        <td>@item.prototype_handsand_pattern</td>
        <td>@item.is_lazer</td>
        <td>@item.prototype_lazer_pattern</td>
        <td>@item.is_pp_spray</td>
        <td>@item.finish_name</td>
        <td>@item.number_of_washes</td>
        <td>@item.wash_code</td>
        <td>@item.is_filler</td>
        <td>@item.is_stamp</td>
        <td>@item.prototype_description</td>
        <td>@item.group_search_name</td>
        <td>@item.request_date</td>
        <td>Pattern Work Required</td>
        <td>@item.need_by_date</td>
        <td>Sew Work Required</td>
        <td>@item.scheduled_date</td>
        <td>Stage Status</td>
        <td>@item.initiatorUser.full_name</td>
        <td>@item.initiatorUser.PhoneNumber</td>
        <td>@item.workgroup.workgroup_name</td>
        <td>@item.pattern_number</td>
        <td>@item.marker_name</td>
        <td>Embroidery (y/n)</td>
        <td>Body Fabric Vendor</td>
        <td>Body Fabric Vendor Style #</td>
        <td>Body Fabric Weight</td>
        <td>Body Fabric Content</td>
        <td>@item.prototype_fabric_information_sap_code</td>
        <td>@item.washEngineerUser.full_name</td>
        <td>@item.washEngineerUser.PhoneNumber</td>
        <td>@item.is_prelines</td>
        <td>@item.cut_number</td>
        <td>Target</td>
        <td>@item.sub_business_unit.sub_business_unit_name</td>
        <td>@item.buyer.buyer_name</td>
        <td>@item.cost_center.cost_center_number</td>
        <td>@item.remake_reason</td>



        @*<td>@item.sub</td>*@
    </tr>

}





我有什么尝试过:



我试图在每个单独的行上设置一个条件...检查是否为null,如果它是null我保持< table>< tbo DY>< TR>< TD>< / TD>< / TR>< / tbody的>< /表>空白。这是一个非常耗时的任务,我正在寻找一个更快的解决方案...我有时候有超过50列的表,并且一遍又一遍地写这个很烦人...



What I have tried:

I have tried to put a condition on each of the individual rows... to check for a null and if its a null i kept the <table><tbody><tr><td></td></tr></tbody></table> blank. This is a very time consuming task and im looking for a faster solution... I have tables that have over 50 columns sometimes and writing that over and over is quite annoying...

推荐答案

您可以尝试使用空合并运算符??而不是向每一行添加ifs。 (内部涉及if语句,但更优雅和可读)。格式是(我只用一行作为例子):



Instead of adding "ifs" to each line, you could try to use the null coalescing operator "??" (which internally involves an if statement, but is much more elegant and readable). The format would be (I take just one line as an example):

<table><tbody><tr><td>@item.prototype_category.prototype_category_name ?? ""</td></tr></tbody></table>





当然,我假设prototype_category_name的数据类型是一个字符串,否则你会有错误。这将使用空字符串替换空值,这将只留下一个空单元格。当然,您可能希望用其他东西替换空字符串以指示该单元格为空...



--AFP



Of course, I'm assuming the data type of prototype_category_name is a string, otherwise you will have an error. This will replace a null value with an empty string, which will just leave an empty cell. Of course, you might want to replace the empty string with something else to indicate that the cell is empty...

--AFP

这篇关于使foreach循环中的值默认为某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆