ASP. NET MVC Razor Syntax (3)

Source: Internet
Author: User

Programming Logic: Execute conditional-based code.

If condition

C # allows you to execute conditional-based code.

If you want to test a condition, you can use the if statement . The IF statement returns TRUE or FALSE based on your test:

  • If statement start code block
  • The condition is in parentheses
  • If the condition is true, execute the code in curly braces
    @{var price=50;} Else condition

    An If statement can contain an else condition .

    The else condition defines the code that executes when the condition is false.

  • @{var price=20;} 

    Note: In the above example, if the price is not greater than 30, the remaining statements are executed.

    Else If Condition

    You can test multiple conditions with the else if condition :

    @{var price=25;} 

    In the example above, if the first condition is true, the first block of code is executed.

    Otherwise, if the next condition is true, the second block of code is executed.

    You can set any number of else if conditions.

    If the IF and else if conditions are not true, the last else code block is executed.

    Switch condition

    A switch code block can be used to test a series of specific conditions:

    @{var Weekday=datetime.now.dayofweek;var Day=weekday. ToString (); var message= "";} 

    The test value (day) is in parentheses. Each specific test condition begins with a case keyword, ends with a colon, and then allows any number of lines of code to end with a break statement. If the test value matches the case value, the line of code is executed.

    The switch code block sets a default case (default:) for the remainder, allowing code to be executed when all cases are not true.

ASP. NET MVC Razor Syntax (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.