The Top 8 Programming Tips in MVC

Unknown | 7:44 PM |

1.You add code to a page using @ Character

The @ character starts inline expressions, single statement blocks, and multi-statement blocks:


<!-- Single statement blocks -->
       @{ var total = 7; }
       @{ var myMessage = "Hello World"; }
       <!-- Inline expressions --><p>
       The value of your account is: @total </p>
       <p>The value of myMessage is: @myMessage</p>
       <!-- Multi-statement block -->
       @{
         var greeting = "Welcome to our site!";
         var weekDay = DateTime.Now.DayOfWeek;
         var greetingMessage = greeting + " Today is: " + weekDay;

        }
        <p>The greeting is: @greetingMessage</p>


The value of your account is: 7
The value of myMessage is: Hello World
The greeting is: Welcome to our site! Today is: Thursday
2. You enclose code blocks in braces
A code block includes one or more code statements and is enclosed in braces.:


<!-- Single statement block. -->
        @{ var theMonth = DateTime.Now.Month; }
        <p>The numeric value of the current month: @theMonth</p>
        <!-- Multi-statement block. -->
        @{
            var outsideTemp = 79;
             var weatherMessage = "Hello, it is " + outsideTemp + " degrees.";
          }
                                                                                                                                                                     
        <p>Today's weather: @weatherMessage</p>

The numeric value of the current month: 9
Today's weather: Hello, it is 79 degrees.
3. Inside a block, you end each code statement with a semicolon
Inside a code block, each complete code statement must end with a semicolon. Inline expressions donot end with a semicolon.:
4. You use variables to store values
You can store values in a variable , including strings, numbers, and dates, etc. You create a new variableusing the var keyword. You can insert variable values directly in a page using @ :


<!-- Storing a string -->
         @{ var welcomeMessage = "Welcome, new members!"; }
         <p>@welcomeMessage</p>
         <!-- Storing a date -->
         @{ var year = DateTime.Now.Year; }
         <!-- Displaying a variable -->
         <p>Welcome to our new members who joined in @year!</p>


Welcome, new members!
Welcome to our new members who joined in 2012!
5. You enclose literal string values in double quotation marks
A string is a sequence of characters that are treated as text. To specify a string, you enclose it in doublequotation marks:


@{ var myString = "This is a string literal"; }
          <p>Literal : @myString</p>
          <!-- Embedding a backslash in a string -->
          @{ var myFilePath = @"C:\MyFolder\"; }
          <p>The path is: @myFilePath</p>


          <!-- Embedding double quotation marks in a string -->
          @{ var myQuote = @"The person said: ""Hello, today is Monday."""; }
          <p>@myQuote</p>


Literal : This is a string literal
The path is: C:\MyFolder\
The person said: "Hello, today is Monday."
6. Code is case sensitive
In C#, keywords ( var, true, if ) and variable names are case sensitive.
The following lines of codecreate two different variables, lastName and LastName.
@{var lastName = 'Smith';var LastName = 'Jones';}
If you declare a variable as var lastName = "Smith"; and if you try to reference that variable in yourpage as @LastName , an error results because LastName won't be recognized.
7. Much of your coding involves objects
An object represents a thing that you can program with — a page, a text box, a file, an image, a webrequest, an email message, a customer record (database row), etc.


<table border="1" style="width:400px">
            <tr>
            <td>Requested URL</td>
            <td>Relative Path</td>
            <td>Full Path</td>
            <td>HTTP Request Type</td>
            </tr>
            <tr>
            <td>@Request.Url</td>
            <td>@Request.FilePath</td>
            <td>@Request.MapPath(Request.FilePath)</td>
            <td>@Request.RequestType</td>
            </tr>
            </table>




8. You can write code that makes decisions
A key feature of dynamic web pages is that you can determine what to do based on conditions. Themost common way to do this is with the if statement (and optional else statement).


@{var result = "";
              if(IsPost)
              {
                  result = "This page was posted using the Submit button.";
              }
              else
              {
                  result = "This was the first request for this page.";
              }
              }
              <input type="submit" name="Submit" value="Submit"/>
              <p>@result</p>


This was the first request for this page.

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....