Mastery of MVC5.0 Chapter5 notes-razor Grammar

Source: Internet
Author: User

1.1. View Declaration Data type

Razor declarations are at the start of the @, such as @model MVC. Models.product declares the data type that the controller creates for the view, so that you can access the data in the view using @modle.property, as follows @model.name, note that the model is capitalized in the first letter of the data type, lowercase

 @model MVC. Models.product@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title>Index</title>@Model. Name</div></body>the corresponding controller method: Public classHomecontroller:controller { PublicProduct myproduct =NewProduct () {ProductID=1, Name="Kayak", Description="A boat for one person", Category="Watersports", Price=275M}; //Get:home         PublicActionResult Index () {returnView (myproduct); }    }

1.2. View template settings

Template file naming convention _ Start, for example _basiclayout.cshtml

@RenderBody

@model MVC. models.product@{    "Product Name";      = "~/views/_basiclayout.cshtml";} @Model. Name

1.3. Using the Viewstart file

If each view page uses the same template file, is it not very troublesome to set layout = "..." For each view page. Use _viewstart.cshtml to specify a default template that can be resolved as follows _viewstart.cshtml

@{    "~/views/_basiclayout.cshtml";}

At this point the index.cshtml page does not specify a template, and the default template uses _basiclayout.cshtml

@model MVC. models.product@{    "Product Name";} @Model. Name

1.4. Using Razor Expressions

The Controller method should be to pass a data object or object collection to the corresponding view instead of the exact data. Remember that the data passed to the view is a collection of objects or objects of the corresponding data type. If there is a view page price.cshtml used to display the price, the corresponding controller method should be return view (myproduct) instead of Return view (Myproduct.price) and then use Razor @ on the view page The Model expression gets the exact data, such as

 is @Model. Name, and it costs $@Model. Price

1.5. Using ViewBag to transmit data to a view

//Controller Method         PublicActionResult Demoviewbag () { viewbag.productcount = 1; Viewbag.expressship = true; Viewbag.applydiscount = false; Viewbag.supplier = null; returnView (myproduct); }//corresponding View@model MVC. models.product@{Viewbag.title="Demoviewbag";}<table> <thead> <tr><th>Property</th><th>Value</th></tr> < /thead> <tbody> <tr><td>Name</td><td> @Model .name</td></tr> & lt;tr><td>price</td><td> @Model .price</td></tr> <tr><td>stock Level& Lt;/td><td>@ViewBag. ProductCount</td></tr> </tbody></table>

1.6. Data transmitted by the Controller as a property value

//Controller Method PublicActionResult Demoviewbag () {Viewbag.productcount=1; Viewbag.expressship=true; Viewbag.applydiscount=false; Viewbag.supplier=NULL; returnView (myproduct); }//corresponding View@model MVC. models.product@{Viewbag.title="Demoviewbag";}<divdata-discount= "@ViewBag. ApplyDiscount" express= "@ViewBag. Expressship" Data-supplier= "@ViewBag. Supplier">The containing element has data attributes</div>Discount1:<input type="checkbox" checked= "@ViewBag. ApplyDiscount"/>Discount2:<input type="checkbox" checked= "@Model. Isdiscount"/>Express:<input type="checkbox" checked= "@ViewBag. Expressship"/>

1.7.Razor conditional expression

Conditional expression starts at @,} ends

Inserting plain text in the view without being surrounded by HTML tags requires @: prefix, for example @:out of the Stock

@model MVC. models.product@{Viewbag.title="Demoviewbag";}<table> <thead> <tr><th>Property</th><th>Value</th></tr> < /thead> <tbody> <tr><td>Name</td><td> @Model .name</td></tr> & lt;tr><td>price</td><td> @Model .price</td></tr> <tr><td>stock Level& Lt;/td> <td>@switch ((int) {viewbag.productcount) { Case 0:                        @: outOf the Stock Break;  Case 1:                        <b>low Stock (@ViewBag. ProductCount) </b> Break; default: @ViewBag. ProductCount Break; }            </td> </tr> </tbody></table>
@model MVC. models.product@{Viewbag.title="Demoviewbag";}<table> <thead> <tr><th>Property</th><th>Value</th></tr> < /thead> <tbody> <tr><td>Name</td><td> @Model .name</td></tr> & lt;tr><td>price</td><td> @Model .price</td></tr> <tr><td>stock Level& Lt;/td> <td> @if (Viewbag.productcount==0) {@:out of Stoc}Else if(Viewbag.productcount = =1)                {                    <b>low Stock (@ViewBag. ProductCount) </b>                }Else{@ViewBag. ProductCount}</td> </tr> </tbody></table>

1.8.Razor traversing arrays and collections

Note: The introduction of namespaces in the View page is the same as the background code: @using namespaces

//Controller Method PublicActionResult Demarray () {product[] array= {                NewProduct () {Name ="Kaya", price=275M},NewProduct () {name="lifejacket", price=48.95M},                NewProduct () {name="Soccer Ball", price=19.50M},                NewProduct () {name="Corner Flag", price=34.95M},            }; returnView (array); }//corresponding View@model MVC. Models.product[] @using MVC. models@{Viewbag.title="Demarray"; Layout="~/views/_basiclayout.cshtml";}@if (Model.length>0){    <table> <thead><tr><th>product</th><th>price</th></tr></ Thead> <tbody>@foreach (Product pinchModel) {                <tr> <td>@p.Name</td> <td>[email protected]</td> </tr>            }        </tbody> </table>}

Mastery of MVC5.0 Chapter5 notes-razor Grammar

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.