Several ways to load partial views in summary view in ASP. NET MVC 4.0
@RenderPage ()
However, it cannot use the Model and ViewData of the original view, and can only be passed by parameters.
1 @RenderPage ("~/shared/component/dialog.cshtml"new" Hello world! ", content="Nani" })
The partial view receives data through the page
1 <div id="Dialog" title="@Page. Title" style= " Display:none; ">2 <p>3 @Page. Title4 </p >5 </div>
@Html. Partial ()
Used to render a partial view as a string
@Html. Partial ("_partialpage1", Model,viewdata) directly returns mvchtmlstring fill
1 @Html. Partial ("component/dialog"nullnew" Title""Hello world! " " content " " Nani? " } })
Razor using ViewBag to get the data passed in the child view
1 <div id="Dialog" title="@ViewBag. Title" style= " Display:none; ">2 <p>3 @ViewBag. Content4 < /p>5 </div>
Passing strongly typed to partial views
1 @{2 varargs =Newdictionary<string,string>();3args["Redirectcontroller"] ="Admin";4args["redirectaction"] ="User";5}6@Html.Partial("_childpartial",args)
_childpartial.cshtml
1 @model dictionary<string,string>2 <div> @Model [" Redirectcontroller"]</div>3 <div> @Model ["redirectaction "] </div>
@RenderPartial ()
The distribution view is written directly to the response output stream, so it can only be placed directly in the code block and cannot be placed in an expression (the return value is void)
RenderPartial does not need to create the Controller's action, and Renderaction needs to create the action to load in the controller. Renderaction will call Contorller's Action first and then render the view, so the page will start a link. If this part of the view is just some simple HTML code, please use RenderPartial. But if this part of the view needs to be rendered by reading the data in the database in addition to the HTML code, it is necessary to use renderaction because it can call the model method in the Action to read the database, render the view and render it, and Renderpartia l have no Action, so I can't do it.
Partial can output content directly, which internally converts the HTML content to a string character (mvchtmlstring), caches it, and finally outputs it to the page at once. Obviously, this conversion process will reduce the efficiency, so usually use renderpartial instead.
@Html. Action ()
Entity class
1 Public classMenu2 {3 PublicList<menuitem> Items {Get;Set; }4 }5 6 Public classMenuItem7 {8 Public stringText {Get;Set; }9 Public stringURL {Get;Set; }Ten}
Controller
1 Publicactionresult MyMenu ()2 {3MvcLearn.Models.Menu m =NewMvcLearn.Models.Menu ();4list<mvclearn.models.menuitem> items =NewList<menuitem>();5Items. ADD (NewMenuItem () {Text ="Baidu", URL ="http://www.baidu.com"});6Items. ADD (NewMenuItem () {Text ="Sina", URL ="http://www.Sina.com" });7Items. ADD (NewMenuItem () {Text ="IBM", URL ="http://www.ibm.com" });8Items. ADD (NewMenuItem () {Text ="Sohu", URL ="http://www.sohu.com" });9M.items =items;Ten returnPartialview (m); One}
Build a partialview-mymenu.cshtml
1 @model MvcLearn.Models.Menu 2 <ul> 3 @foreach (var in model.items) 4 { 5 <li><a href="@item. URL"> @item. Text</a></li> 6} 7
Call the action in the page to generate the view:
1 @Html. Action ("MyMenu")
Pass in Parameters when action is called
1 @Html. Action ("MyMenu"newnew"haha" , Url =<a href="http://www.ms.com">http://www.ms.com </a>}})
@Html. Renderaction ()
1 @{html.renderaction ("MyMenu")}
The Action is also a direct output, and, like Partial, there is a conversion process. Renderaction output directly to the current HttpContext efficiency.