5

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?

7 Answers 7

10

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>
Sign up to request clarification or add additional context in comments.

2 Comments

I like this too. I reusable option.
this should be accepted as answer because of reusablity
7
<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>

2 Comments

I like that you provided two examples, the first one more inline and the second with the if/else block. But in the if/else block example, how to get away from adding an additional/redundant empty span tag?
Thank you codingbiz. The number of examples has given me a better understanding of Razor.
4

You can do this:

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

Comments

2

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.

Comments

1

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>

Comments

1

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; }

Comments

0

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.

1 Comment

The problem here is that if the server returns a validation error the page will refresh with the errors listed and your field will again be blank, even if the user had entered a value...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.