Razor Tutorial – How to Use Variables and Datatypes

In this chapter you will learn
  • How to define Variables with DataTypes in Razor Markup
  • Converting Datatypes
  • Programming Example

How to define Variables and DataTypes in Razor Markup

There are two types of variable initialization in Razor Markup.
  1. Implicitly Typed Variable : Using var keyword you can initialize any type of variable in razor syntax. It is known as Implicitly-typed variable. You must initialize the variable with value if it is var variable.

    Example:
    var s; @* Wrong way. Rasie Error *@
    var s = "Hello"; @* Correct Way *@

  2. Explicitly Typed Variable : This variable is defined with data typed as int, string, float etc.

Programming Example

<html>
    <head>
        <title></title>
    </head>
    <body>
        @{   
             @* //Using var Keyword – Implicitly Typed *@          
            var message = "Add Two Numbers";
            var num1 = 5;
            var sum = num1 + num1;
            <h5>@message</h5>
            <h5>@num1 + @num1 = @sum</h5>
 
            @* //Using Explicit Data Type *@
            int i = 10;
            int result = i * i;
            <h5>Using Explicit Data Types</h5>
            <h5>@i  x @i  = @result</h5>
        }
    </body>
</html>


Output
Add Two Numbers
5 + 5 = 10
Using Explicit Data Types
10 x 10 = 100

 

DataTypes

You can use all the C# or VB data types in Razor Syntax as the way you use in programming. But the process of converting data type is different and you must know the concept of conversion in Razor markup.

Converting Data Types

1. Converting Integer – Convert string that has value like integer, example "239" or "45". There are two methods for converting int. IsInt() Method and AsInt() Method.
IsInt() – It checks whether provided variable has integer value or not.
AsInt() – It converts variable into integer datatypes.


Example Converting Integer
<html>
    <head>
        <title>Converting Integer</title>
    </head>
    <body>
        @{            
            var number = "525";
            int num;
            @* //This line will raise error because you cannot store string into int variable *@
            @* //num = number; *@
            if(number.IsInt()==true)
            {                
                num=number.AsInt();
                <h5>@num  is Integer Value</h5>
            }
            else
            {
                <h5>@number  is Not Integer</h5>
            }
        }
    </body>
</html>


Output
525 is Integer Value


2. Converting Bool – Converts string "true" and "false" into Boolean type. There are two methods for converting Bool. IsBool() Method and AsBool() Method.
IsBool() – It checks whether provided variable has Boolean value or not.
AsBool() – It converts variable into Boolean datatypes.


Example Converting Bool
<html>
    <head>
        <title>Converting Bool</title>
    </head>
    <body>
        @{
            var b = "false";
            Boolean bln;
            if(b.IsBool()==true)
            {
                bln=b.AsBool();
                <h5>@bln</h5>
            }
        }
    </body>
</html>

Output
False

3. Converting Float – Converts a string that has a decimal value like "2.5" or "6.123". There are two methods for converting Float. IsFloat() Method and AsFloat() Method.
IsFloat() – It checks whether provided variable has Float value or not.
AsFloat() – It converts variable into Float datatypes.


Example Converting Float
<html>
    <head>
        <title>Converting Float</title>
    </head>
    <body>
        @{
            var v = "2.63";
            float f;
            if(v.IsFloat()==true)
            {
                f=v.AsFloat();
                <h5>@f</h5>
            }
        }
    </body>
</html>


Output
2.63

4. Converting Decimal - Converts string that has a decimal value like "2.6" or "6.1". There are two methods for converting Decimal. IsDecimal() Method and AsDecimal() Method.
IsDecimal() – It checks whether provided variable has Decimal value or not.
AsDecimal() – It converts variable into Decimal datatypes.


Example
<html>
    <head>
        <title>Converting Decimal</title>
    </head>
    <body>
        @{
            var v = "2.63";
            decimal d;
            if(v.IsDecimal()==true)
            {
                d=v.AsDecimal();
                <h5>@d</h5>
            }
        }
    </body>
</html>


Output
2.63

5. Converting DateTime – Converts string that has value in date time format. There are two methods for converting DateTime. IsDateTime() Method and AsDateTime() Method.
IsDateTime() – It checks whether provided variable has DateTime value or not.
AsDateTime() – It converts variable into DateTime datatypes.


Example
<html>
    <head>
        <title>Converting DateType</title>
    </head>
    <body>
        @{
            var v = "24/08/2016";
            DateTime d;
            if(v.IsDateTime()==true)
            {
                d=v.AsDateTime();
                <h5>Date is : @d</h5>
            }
        }
    </body>
</html>


Output
Date is : 24-08-2016 AM 12:00:00

6. Converting StringToString() method converts any data type into string.


Example
<html>
    <head>
        <title>Converting String</title>
    </head>
    <body>
        @{
            var num = 2342;
            DateTime d=DateTime.Now;
            bool b = true;
 
            <h5>@num.ToString() is String Now.</h5>
            <h5>@d.ToString() is String Now.</h5>
            <h5>@b.ToString() is String Now.</h5>
        }
    </body>
</html>

Output
2342 is String Now.
24-08-2016 PM 08:21:00 is String Now.
True is String Now.

Summary

In this chapter you learned to define variable and converting data types in Razor Syntax. In the next chapter you will learn to use Operators in Razor Syntax.
 

Share your thought