slolife
slolife

Reputation: 19870

ASP.NET MVC and Razor to display integer or blank when 0

If I have an integer value that I want to display on a page, I can do that a number of ways:

<span>@Html.DisplayFor(modelItem => item.UserId)</span>
<span>@item.UserId</span>

But what is the best way to convert that to displaying the value IF UserId != 0. But if UserId == 0, display an empty string. Is there a way to do it right in Razor syntax or do I need to head to code?

Upvotes: 5

Views: 22104

Answers (7)

Robert C
Robert C

Reputation: 11

I did some more global solution, display blank for: - integer: 0 - decimal: 0.0 - string: "0x0" or "0x0x0"

Extension class:

public static class DataTypesExtension
{
    static string[] Keywords = { "0", "0.0", "0.00", "0.000", "0.0000", "0,0", "0,00", "0,000", "0,0000", "0x0", "0x0x0" };
    public static string BlankIfZero(this object n)
    {
        string ret = string.Empty;

        if (n == null)
            return ret;

        string sn = n.ToString().Replace(" ", "");


        if (!DataTypesExtension.Keywords.Contains(sn))
            ret = n.ToString();

        return ret;
    }
}

Display template:

@model object

@using MyProject.Extensions;

@{
    string val = Model.BlankIfZero();
    @val;        
}

And in View Model:

[UIHint("BlankIfZero")]
public int? MyIntProperty { get; set; }

[UIHint("BlankIfZero")]
public decimal? MyDecimalProperty { get; set; }

[UIHint("BlankIfZero")]
public string MyStringProperty { get; set; }

Upvotes: 1

MelloG
MelloG

Reputation: 1064

On my case, i found this to be easier:

$(document).ready(function () {
        $("#myField").val("");
}

Not sure if this is wahat you were looking for, but worked fine for me.

Upvotes: 0

Karthikeyan P
Karthikeyan P

Reputation: 1267

This is another way to display integer or blank when model value is 0.

Model:

[Display(Name = "User Id")]
public int UserId { get; set; }

View:

<span>
    @Html.DisplayNameFor(m => m.UserId)
</span>
<span>
    @Html.TextBoxFor(m => m.UserId, new { @Value = (Model.UserId > 0 ? Model.UserId.ToString() : string.Empty) })
</span>

Upvotes: 1

codingbiz
codingbiz

Reputation: 26386

<span>@((item.UserID == 0) ? "" : @item.UserID.ToString())</span>

OR

<span>@if(item.UserID == 0) { <span></span> }
   else { @Html.DisplayFor(m => item.UserID); }
</span>

I think you could do this with one if condition

<span>@if(item.UserID != 0) { @Html.DisplayFor(m => item.UserID); } //the browser would render empty string by itself

To render content without putting the redundant (as you said) <span>, use the @: - MVC3 Razor: Displaying html within code blocks and @: for displaying content

<span>
   @if(item.UserID == 0) {  } //this is redundant too
   else { @Html.DisplayFor(m => item.UserID); 
   }
</span>

Note that I have moved the } to next line. ASP.Net MVC did not accept it

<span>
   @if(item.UserID == 0) { @:Some content with no html tag wrapper 
   }
   else { @Html.DisplayFor(m => item.UserID); 
   }
</span>

Upvotes: 7

Michal B.
Michal B.

Reputation: 5719

I think the best and most reusable would be to create a template.

So you create e.g. UserId.vbhtml in ~/Views/Shared/DisplayTemplates with the following content:

in the template you build all the logic, which would be something like (I am typing without checking and I usually code in VB, so there may be mistakes):

@model int
@((Model == 0) ? "" : @Model)

And then in the code you use: @Html.DisplayFor(modelItem => item.UserId, "UserId")

You can also put UIHint attribute over UserId in your class: [UIHint("UserId")] and then you do not need to provide the template name.

Upvotes: 2

Sebastian Leth
Sebastian Leth

Reputation: 446

Create an extension method for int:

public static class IntExtensions
{
    public static string EmptyIfZero(this int value)
    {
        if(value == 0)
            return string.Empty;

        return value.ToString();
    }
}

... and then in your Razor view, do:

<span>@item.UserId.EmptyIfZero()</span>

Upvotes: 10

Gromer
Gromer

Reputation: 9931

You can do this:

<span>@(item.UserId != 0 ? item.UserId.ToString() : string.Empty)</span>

Upvotes: 4

Related Questions