ASP. net mvc learning notes on the third day, mvc learning notes

Source: Internet
Author: User
Tags actionlink

ASP. net mvc learning notes on the third day, mvc learning notes
Layout:If the Layout page is not used, set the Layout attribute to null. @ {Layout = null ;}Use the default layout page:In the Add View dialog box, select the layout page (the layout Page name text box is blank) to automatically create several directories and layout pages. The _ ViewStart. cshtml page contains the default configuration of all views. _ ViewStart. cshtml: @ {Layout = "~ /Views/Shared/_ Layout. cshtml ";} The Layout page contains the HTML content of all pages that use this Layout page. The RenderBody method of the base class WebPageBase presents the content of the content page, so it defines the location where the content is placed. <! DOCTYPE html> @ RenderSection ("PageNavigation", required: false) // use the partition</Div> <div> @RenderBody ()</Div> <footer> Sample code for custom sion C # </footer> </body>   Use some views in the server code:Public ActionResult UseAPartialView1 () {return View (new EventsAndMenus ();} use the HTML Helper Method Html. PartialSome views can be displayed. Html. Partial returns an MvcHtmlString. When the Razor syntax is used, the string is written as the content of the div element. The name of the view that the first parameter of the Partial method receives. If the second parameter is used, Partial allows the model to be passed. If no model is passed, some views can access the same model as the view. Here, the view uses the EventsAndMenus model. Some views only use part of the model, and the type of the model used is IEnumerable <Event> @ Model MVCtest. Models. EventsAndMenus@ {ViewBag. title = "UseAPartialView1"; ViewBag. eventsTitle = "Live Events" ;}< h2> Use A PartialView1 @ Html. Partial ( "ShowEvents", Model. Events)</Div> another way to present partial views in a view is to use the HTML Helper method Html. RenderPartial, which is defined as return void. This method directly writes the content of some views to the response stream. In this way, you can use RenderPartial In the Razor code block.   Return some views from the ControllerThe PartialView method transmits the model containing the event list to some views: // some views are returned from the Controller. public ActionResult UseAPartialView2 () {return View ();} public ActionResult ShowEvents () {ViewBag. eventsTitle = "Live Events "; Return PartialView (new EventsAndMenus (). Events );} View UseAPartialView2 calls the controller by calling HTML Helper method Html. Action. The action name is ShowEvents, which uses the same controller as the view. Other controllers and parameters can be passed as methods within the Action Method: @ model MVCtest. models. eventsAndMenus @ {ViewBag. title = "Use A Partial View2";} @ Html. Action ( "ShowEvents ")</Div>   Call some views in JQuerySome views can also be directly loaded in client code. Next: Click an event when the event handler is linked to a button. In the click event handler, a GET request for/ViewsDemo/ShowEvents is sent to the server. This request returns a partial view. The results of some views are placed in the div element named events. @ Model MVCtest. models. eventsAndMenus @ {ViewBag. title = "UseAPartialView3";} <script type = "text/javascript" >$ (function () {$ ("# getEvents "). click (function () {$ ("# events "). load ("/ViewsDemo/ShowEvents ");});}); </script>

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.