Razor View Tips, Tricks & Hacks

Razor View Tips, Tricks & Hacks

Added by Nasir Mahmood updated on Thursday, October 24, 2013

Code Nuggets:

Code Nuggets are simple expressions that evaluate and rendered inline and always return markup for the view to render.

@Html.ActionLink("About Us","About")

Code Block:

Razor code block can be defined in any section by @{}, Unlike Code Nuggets Code Blocks do not render anything to the view. Instead, they allow you to write arbitrary code that requires no return value.

@{
    int x=5;    
    string message="Hello there!";
}

Variables defined within Code Blocks may be used by Code Nuggets.

@for (var i = 0; i < @x; i++)
{
    <div>@message</div>
}

@( ) Statement:

@( ) statement is used to wrap a small code.

Posted By:@(Model.PostBy)

@: Statement:

Contents that follows @: Statement treated as text not as code so doesn’t execute.

@if (User.Identity.IsAuthenticated)
{
    @:Hello, @User.Identity.Name!
}
else
{
    @:Please login
}

<text> Statement:

<text> Statement is useful when you want to write multiple lines of text.

@if(!User.Identity.IsAuthenticated)
{
    <text>
        Guests are not allowed to view this content.
        Kindly @Html.ActionLink("login", "Login") to view content.
    </text>
}

Razor Comments:

Anything between @* and *@ is consider as comments in Razor view engine.

@*
    Simple Text
    <span>This is also comment</span>
    @for(var i=0;i<4;i++) {
                           @comment
                           }
                           *@

@ Character Escape:

Sometimes it's required to write @ character in view, since @ is razor syntax and it will generate parse error, the solution of this problem is to escape by

@@

@Html.Raw(htmlasstring):

@Html.Raw(htmlasstring) is used to render html encoded string as output

@Html.Raw(<b>bold text</b>)

Razor and HTML Code As Output:

Sometimes it's required to display razor and html code as output i.e writing html or razor tutorial. we can't use @: and <text> because razor treat text inside < > as html tag and render them. For simple text you can use @("inputstring") as


public List@("<PostModel>") GetPosts()

For complex code use @Html.Raw() and @Html.Encode() methods as


@{
    var code=@"
    @model MyModel
    <div>
    @foreach (var item in Model.ItemList)
    {
        <div>
            <h3>@item.Name</h3>
            <p>@item.Detail</p>
        </div>    
    }
    </div>";
    @Html.Raw(Html.Encode(code))
}

Converting and Testing Data Types:

Although ASP.NET can usually determine a data type automatically, sometimes it can't. So it's helpful to test to see what type of data you might be working with.

Converting and Testing Int:

Method AsInt() is used to convert data type to Int and IsInt() is used to test either data type is Int or not

var input = "123";
if(input.IsInt())
{
    var newvar = input.AsInt();
}
Converting and Testing Bool:

Method AsBool() is used to convert data type to Bool and IsBool() is used to test either data type is Bool or not

var input = "true";
if(input.IsBool())
{
    var newvar = input.AsBool();
}
Converting and Testing decimal:

Method AsFloat(),AsDecimal is used to convert data type to decimal and IsFloat(),IsDecimal() is used to test either data type is decimal or not. In ASP.NET, a decimal number is more precise than a floating-point number

var input = "123.456";
if(input.IsFloat())
{
    var newvar = input.AsFloat();
}
Converting and Testing DateTime:

Method AsDateTime() is used to convert data type to DateTime and IsDateTime() is used to test either data type is DateTime or not

var input = "12/27/2012";
if(input.IsDateTime())
{
    var newvar = input.AsDateTime();
}

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected