Conditional Logic and Loops in MVC

Unknown | 8:55 PM |

Conditional Logic and Loops
ASP.NET server code lets you perform tasks based on conditions and write code that repeats statementsa specific number of times (loops).
Testing Conditions
To test a simple condition you use the if statement, which returns true or false based on a test youspecify:


 @{var showToday = true;if(showToday){@DateTime.Today;}}

        @{
            showToday = false;
            if(showToday)
            {
                @DateTime.Today;
            }
            else
            {
                <br />
                <text>Sorry!</text>
            }}



9/13/2012 12:00:00 AM
Sorry!
If else Code
You can add multiple conditions using an else if block:


@{
            var theBalance = 4.99;
           if(theBalance == 0)
           {
                      <p>You have a zero balance.</p>        
           }
            else if (theBalance > 0 && theBalance <= 5)
            {
            <p>Your balance of $@theBalance is very low.</p>
            }
            else
            {
            <p>Your balance is: $@theBalance</p>
            }
            }



Your balance of $4.99 is very low.
Switch case Code
To test a large number of conditions, use a switch block:


 @{
                var weekday = "Wednesday";
                var greeting = "";
                switch (weekday)
                {
                    case "Monday":
                        greeting = "Ok, it's a marvelous Monday";
                        break;
                    case "Tuesday":
                        greeting = "It's a tremendous Tuesday";
                        break;
                    case "Wednesday":
                        greeting = "Wild Wednesday is here!";
                        break;
                    default:
                        greeting = "It's some other day, oh well.";
                        break;
                }
                <p>Since it is @weekday, the message for today is: @greeting</p>
                }



Since it is Wednesday, the message for today is: Wild Wednesday is here!
For Looping Code
 @for(var i = 10; i < 21; i++){<p style="font-size: @(i + "pt")">My font size is now: @i</p>}

My font size is now: 10
My font size is now: 11
My font size is now: 12
My font size is now: 13
My font size is now: 14
My font size is now: 15
My font size is now: 16
My font size is now: 17
My font size is now: 18
My font size is now: 19
My font size is now: 20
For each Looping Code
<ul>@foreach (var myItem in Request.ServerVariables){<li>@myItem</li>}</ul>
ALL_HTTP
ALL_RAW
APPL_MD_PATH
APPL_PHYSICAL_PATH
AUTH_TYPE
AUTH_USER
AUTH_PASSWORD
LOGON_USER
REMOTE_USER
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH
CONTENT_TYPE
GATEWAY_INTERFACE
HTTPS
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID
INSTANCE_META_PATH
LOCAL_ADDR
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_PORT
REQUEST_METHOD
SCRIPT_NAME
SERVER_NAME
SERVER_PORT
SERVER_PORT_SECURE
SERVER_PROTOCOL
SERVER_SOFTWARE
URL
HTTP_CONNECTION
HTTP_ACCEPT
HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE
HTTP_COOKIE
HTTP_HOST
HTTP_REFERER
HTTP_USER_AGENT
HTTP_DNT
While Looping Code
@{var countNum = 0;while (countNum < 50){countNum += 1;<p>Line #@countNum: </p>}}

Line #1:
Line #2:
Line #3:
Line #4:
Line #5:
Line #6:
Line #7:
Line #8:
Line #9:
Line #10:
Line #11:
Line #12:
Line #13:
Line #14:
Line #15:
Line #16:
Line #17:
Line #18:
Line #19:
Line #20:
Line #21:
Line #22:
Line #23:
Line #24:
Line #25:
Line #26:
Line #27:
Line #28:
Line #29:
Line #30:
Line #31:
Line #32:
Line #33:
Line #34:
Line #35:
Line #36:
Line #37:
Line #38:
Line #39:
Line #40:
Line #41:
Line #42:
Line #43:
Line #44:
Line #45:
Line #46:
Line #47:
Line #48:
Line #49:
Line #50:

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 ....