ASP. net mvc learning notes the next day, mvc learning notes

Source: Internet
Author: User

ASP. net mvc learning notes the next day, mvc learning notes
Create ViewIt is best to specify the HTML code returned to the client through the view. Views are defined in the Views folder. The view of the ViewsDemo Controller requires a ViewsDemo sub-Directory, which is the view convention. You can put the views used by multiple controllers (and some special views used by multiple views) in the Shared directory. In the Code Editor, right-click the Index method and choose Add | View to create a View. Transmit data to the view: the controller and view run in the same process. The view is directly created in the controller, which facilitates data transmission from the Controller to the view. You can use ViewDataDictionary. The simpler syntax is to use the ViewBag attribute. ViewBag is a dynamic type. The runtime specifies any attribute name to pass data to the view: public ActionResult PassingData ()
{
ViewBag. MyData = "Hello from the controller"; return View () ;}to access the data transmitted by the controller, you can use ViewBag in a similar way. Similar to the Controller base class, the ViewBag attribute is defined in the base class WebViewPage of the view: <body>
<Div>
<Div> @ ViewBag. MyData </div> </body>

Razor syntax: Razor uses @ self-payment for migration. The code after @ is C. Razor can automatically detect the end position of the C # code. When using the Razor syntax, You need to distinguish the return value statement from the non-return value method. The returned value can be used directly. If you call a method without return values or specify other statements without return values, you must use the Razor code block: The following defines a string variable: @ {string name = "Angela ";} With the migration characters, you can use the variables in simple Syntax:<Div> @ (name), Stephen </div> the foreache block can also define the Razor code block @ foreach (var item in Model) {<li style = "font-size: 50px "> @ item. text </li>} Strong type viewUsing ViewBag to pass data to a view is only one way. Another way is to pass the model to the view. Here, a new list of Menu items is created, and the list is passed to the View method of the base class Controller. Public ActionResult PassingAModel () {var menus = new List <Menu> {new Menu {Id = 1, Text = "Schweinsbraten mit Knodel und", Price = 6.9, category = "Main"}, new Menu {Id = 2, Text = "Shanghai is good", Price = 6.9, Category = "Vegetarian"}, new Menu {Id = 3, text = "Beijing huanyingni", Price = 6.9, Category = "Main" },}; return View (menus );} The information in the Action method can be used as a model in the View:@ Using MVCtest. Models @ model IEnumerable <Menu >@{ Layout = null ;}<! DOCTYPE html>

Related Article

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.