admin管理员组文章数量:1391929
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 |2 Answers
Reset to default 1You 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.
本文标签: cASPNET MVC and Razor to not display anything if current yearStack Overflow
版权声明:本文标题:c# - ASP.NET MVC and Razor to not display anything if current year - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744633982a2616710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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