Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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....
HTML
@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>

}


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...
Posted
Updated 8-Apr-16 18:40pm
v2
Comments
Member 12362975 8-Apr-16 17:47pm    
This is the foreach loop.... it wouldnt paste in original post

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

<td>
<div class="btn-group btn-group-sm btn-group-solid">


</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>
Sergey Alexandrovich Kryukov 8-Apr-16 17:56pm    
All your <td> <tr> are not statement. You need to grasp the idea of programming: never repeat anything.
—SA
Member 12362975 8-Apr-16 17:58pm    
what? this doesnt make sense??english would be appreciated. I know all of <td> are not using model... I know i typed out values for some... mainly because those values are not available to retrieve just yet....
Sergey Alexandrovich Kryukov 8-Apr-16 21:59pm    
Oh, sorry. Yes, it's a good idea to use English. :-)
Well, I clarified some of it in my comment below. I also meant that your quite reasonable "I don't want to have to go to each individual row and say..." still assume that it's an option, even if it's bad. But programming is the field where it's rejected from the very beginning. Adding some Razor syntax with C# code should be considered as a convenient ad-hoc technique, where you customize/parametrize generation of HTML in a HTTP response by injecting some server-side generated code in some elements. But should you repeat some elements, (<p>, <td>, <tr) even a small number of times, this ad-hoc, way too manual technique should be rejected. One solution would be to generate the whole fragment of HTML solely in C#, not interlacing C# and HTML code...
—SA
Matt T Heffron 8-Apr-16 19:22pm    
Sergey, this looks like valid ASP.NET syntax (for the part of interest).
(And I fixed the markup...)
(No, I'm not any kind of ASP.NET guru. I just had a suspicion! ;-) )

1 solution

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):

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


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
 
Share this answer
 
Comments
Member 12362975 9-Apr-16 0:49am    
so how would i do <td>@item.prototypecategory.prototype_category_name ?? </td>

and have the <td></td> empty if the value is empty? i mean where do i basically say if it is null then make it an empty <td>? It is part of requirements to make it empty.
andres_fprado 9-Apr-16 0:54am    
In Razor, what you enter after the "@" must be a valid C# expression. In this case, I'm proposing you use the "??" operator. This is a binary operator, which translates essentially as:

x = a ?? b

is equivalent to

if(a != null)
x = a;
else
x = b;

Therefore, you must have a valid C# expression after the ?? operator. For example, if you want the <td></td> to empty, you would put ?? "" after the expression you are evaluating (which means, 'if the expression is null, replace with an empty string).
Member 12362975 9-Apr-16 0:56am    
so something like this?

<td>@item.prototypecategory.prototype_category_name ?? "" <td></td></td>
andres_fprado 9-Apr-16 0:59am    
No, you don't need the second <td></td> at all. You would just write:

<td>@item.prototypecategory.prototype_category_name ?? "" </td>

In Razor, the first <td> will always be emitted (written to the output). Then, the @ means to evaluate the C# expression and emit the result (which will be either the value of your variable, if not null, or an empty string otherwise). Finally, the closing </td> will always be emitted.

So, if the variable's value was "test", you would get <td>test</td>, and if it was null, you would simply get <td></td>.
Member 12362975 9-Apr-16 1:03am    
so then how would the expression be if have a value other than a blank <td>?

for instance lets say the value that the item is null. since it is null i want a <td>no</td> to be displayed on the screen?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900