Asp.net mvc3 Summary

Source: Internet
Author: User

Razor view engine-basic syntax-basics-all strings starting with @ or @ {/* Code body */} (no spaces can be added directly to @ and) ASP.. NET engine. each line of code in @ {/* Code body */} must end with ";", for example, @ {var I = 10; var y = 20 ;} @ xxx does not need to use ";" as the Terminator, for example, @ I outputs 10 @ y; outputs 20; letters in the code area are case-sensitive. character Type constants must be enclosed by "", for example, @ {string str = "my string ";} -Note-if you need to output the "@" character on the page, you can use the html ascii code @. Of course, Razor also provides the smart analysis function: if the first character of @ is not a blank character, ASP. NET will not process it, for example: <p> text @ I xx </p> output text @ I xx single-line Syntax: @ {var I = 10;} multi-line Syntax: @ {var I = 10; Var y = 20;} 1. when using local variables, Razor does not support access modifiers (such as public and private, which makes no sense). It defines local variables @ {var total = 7;} on a single line ;} @ {var myMessage = "Hello World";} defines the local variable @ {var greeting = "Welcome to our site on multiple lines! "; Var weekDay = DateTime. now. dayOfWeek; var greetingMessage = greeting + "Today is:" + weekDay;} use The variable <p> The value of your account is: @ total </p> <p> The value of myMessage is: @ myMessage </p> Note: Variable concatenation output @ {var I = 10 ;} <p> text @ I text </p> will output text 10 text. But what if you want to output text10text? <P> text @ {@ I} text </p> <p> text @ I text </p>: output text @ I text <p> text @ itext </ p> output text @ itext <p> text @ itext </p> will return an error. If the method name of the output variable is the method name, it does not need to be included in @ {}. for example: <p> text@ I .ToString () text </p> using variable objects can be written directly: @ var1 @ var2 @ myObject. xx2. use logic processing @ {if (xx) {// do something} else {// do anything} 3. at @{...} use html Tag @ {<p> text </P> <div> div1 </div>} 4 internally. at @{...} the internal output text uses @: to output a single line: @ {@: This is some text @: This is text too @: @ I can also output variables} use <text/> to output multiple rows: @ {<Text> tomorrow is goodsome girl is nice </text>} 5. at @{...} internally use annotation @ {// single line annotation var I = 10; // defg} @ * multi-line comment * {@ * multi-line comment * @ var I = 10; @ * asdfasf * @} <! -- You can also use the C # default/*... */--> @ {/* multi-line comment. http://www.52mvc.com */} If it is used inside @ {...} <! --> Comment, it is output to the page. If it is in <! --Asp.net mvc --> If @ variable is used internally, @ {<! -- Time now: @ DateTime. Now. ToString () -- >} output: <! -- Time now: 4/9/2011 --> 6. type conversion AsInt (), IsInt () AsBool (), IsBool () AsFloat (), IsFloat () AsDecimal (), IsDecimal () AsDateTime (), IsDateTime () ToString () example: @ {var I = "10" ;}< p> I = @ I. asInt () </p> <! -- Output I = 10 --> 7. Use a loop <! -- Method 1 --> @ for (int I = 10; I <11; I ++) {: @ I} <! -- Method 2 -- >@{ for (int I = 10; I <11; I ++) {// do something} <! -- While similarly --> http://blog.csdn.net/jhl52771/article/details/8488232

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.