Johnny Grimes
Johnny Grimes

Reputation: 411

Shorthand for Case in Razor syntax

I have a class like the one below ReportStatusConstants. I use it to store the report status in a database in column.

What is the best way to convert it back to text when displaying in my view? There must be a shorthand version of what I'm doing below?

currently I have something like this:

Razor:

@switch ((int)ViewBag.status)
{
    case 1:
        <p>Completed</p>
        break;
    case 0:
            <p>New</p>
        break;

}

My Class:

public class ReportStatusConstants
{

    public const int New = 0;
    public const int Complete = 1;
    public const int Rejected = 2;

}

here's a fiddle: https://dotnetfiddle.net/Y0vYan

Upvotes: 0

Views: 149

Answers (1)

user5993582
user5993582

Reputation:

You can use enum for that,

public enum ReportStatusConstantsEnum
{
    New = 0,
    Complete = 1,
    Rejected = 2,
}

then in controller:

@ViewBag.status = ReportStatusConstantsEnum.New;

and in View:

@ViewBag.status.ToString()

here's a fiddle: https://dotnetfiddle.net/vJowOd

Upvotes: 1

Related Questions