最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET MVC and Razor to not display anything if current year - Stack Overflow

programmeradmin1浏览0评论

I have this code in a .cshtml file

@if (Model.YearsInBusiness > 1)
{
    <div class="in-business mt15" style="width:100%; float:left">
        <span class="badge" style="color:white; background-color:black; font-size: 16px;">@Model.YearsInBusiness</span> Years in business
    </div>
}
else if (Model.YearsInBusiness == 1)
{
    <div class="in-business mt15" style="width:100%; float:left">
        <span class="badge" style="color:white; background-color:black; font-size: 16px;">@Model.YearsInBusiness</span> Year in business
    </div>
}
else if (Model.YearsInBusiness == 0)
{
    <div class="new-company" style="width:100%; float:left">
        New company
    </div>
}

First two parts are working fine:

  • if data entry year is > 1, then it's okay
  • If data entry year = 1, it's also okay

But now I have an issue with 3rd part, if data entry year is current year, is calculated as 0, but also if there isn't and data entry into the field on back-end (mongoDB), it's still 0.

I found that by adding the following code to the template file:

<p>Years in business: @Model.YearsInBusiness</p>

When I open a page that have year entry 2025, I get

Years in business: 0

And if I open a page with no year entry , I also get

Years in business: 0

which means null is somehow treated as 0...

Is there any way to fix this 3rd part? Because it needs to display "New company" only for those companies that have value for 2025 (current year) in their backend.

And those companies that don't have any value in the backend, should not display anything on front-end.

The issue is I don't have access to the source code and controllers to modify this.

I have this code in a .cshtml file

@if (Model.YearsInBusiness > 1)
{
    <div class="in-business mt15" style="width:100%; float:left">
        <span class="badge" style="color:white; background-color:black; font-size: 16px;">@Model.YearsInBusiness</span> Years in business
    </div>
}
else if (Model.YearsInBusiness == 1)
{
    <div class="in-business mt15" style="width:100%; float:left">
        <span class="badge" style="color:white; background-color:black; font-size: 16px;">@Model.YearsInBusiness</span> Year in business
    </div>
}
else if (Model.YearsInBusiness == 0)
{
    <div class="new-company" style="width:100%; float:left">
        New company
    </div>
}

First two parts are working fine:

  • if data entry year is > 1, then it's okay
  • If data entry year = 1, it's also okay

But now I have an issue with 3rd part, if data entry year is current year, is calculated as 0, but also if there isn't and data entry into the field on back-end (mongoDB), it's still 0.

I found that by adding the following code to the template file:

<p>Years in business: @Model.YearsInBusiness</p>

When I open a page that have year entry 2025, I get

Years in business: 0

And if I open a page with no year entry , I also get

Years in business: 0

which means null is somehow treated as 0...

Is there any way to fix this 3rd part? Because it needs to display "New company" only for those companies that have value for 2025 (current year) in their backend.

And those companies that don't have any value in the backend, should not display anything on front-end.

The issue is I don't have access to the source code and controllers to modify this.

Share Improve this question edited 2 days ago marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 14 at 20:21 AcidburnsAcidburns 1433 silver badges13 bronze badges 4
  • You should handle this scenario where you calculate the value of YearsInBusiness Set it to -1 if there is no entry in the database or whatever storage media you use for your data – Steve Commented Mar 14 at 20:41
  • YearsInBusiness are set in the backend in mongoDB , there is a field: Year founded , where we set/fill the year as number. But probably is calculated in the source code or in some controller of the .NET app, where we don't have access to. So that's why i ask if there is other solution to fix this? – Acidburns Commented Mar 14 at 21:26
  • 1 This doesn’t seem like a Razor issue… Show your page model where the wrong value appears (0 instead of null). Ints are value types and cannot be null unless explicitly specified as nullable (int?). The default value for ints is 0. Probably this messes things up somewhere. – BenderBoy Commented Mar 15 at 1:25
  • I see you don’t have access to the PageModel… Perhaps you have access to the YearFounded property? Then you could calculate the difference yourself. Otherwise, whoever is responsible for the Model probably needs to fix it. – BenderBoy Commented Mar 15 at 1:27
Add a comment  | 

2 Answers 2

Reset to default 1

You used most probably int or other numeric value type, which defaults to 0.

SO when you are getting data from database and respective field in database is null, in code respective property will default to 0.

What you need to change is to make the numeric value type nullable, for example int? YearsInBusiness. This way you will be able to check if YearsInBusiness has value, and if it has, act appropriately.

The issue is I don't have access to the source code and controllers to modify this.

Check other properties in the Model to see if it is possible to determine which companies have any value based on those properties. If these properties exist, you can according to them to display "New company" or "Empty" (add an if-else condition inside the 3rd part if block), otherwise, you can't do anything except modify the backend.

发布评论

评论列表(0)

  1. 暂无评论