Html.partialview (), HTML. Renderpartial,html.action.html.renderaction Auxiliary method

Source: Internet
Author: User

Html.partial (), returns the HTML string. parameter is a partial view

Html. RenderPartial (), does not return an HTML string, directly outputs the response stream. parameter is a partial view

It is generally used for the model that is already present in the main view or through the current controller action method viewbag the dynamic object has been brought back into view. The general form is @Html. Partial ("_particalview", Model). or @{html. RenderPartial ("_particalview", Model);}

Html. RenderPartial () performance is generally better than html.partial ().

Html.action (), returns an HTML string, calls the action method on the controller (typically [childaction]), and returns a partial view by the action method. parameter is the action method.

Html.renderaction () does not return an HTML string, directly outputting the response stream. parameter is the controller action method.

Html.renderaction () performance is generally better than HTML. Action ()

Generally used in the main view does not exist in the model, you need to invoke the operation method to obtain the model. The general form is @Html. Action ("_someaction", Routedata), @{html.renderaction ("_someaction", Routedata), routedata);} Used to pass parameters to the controller action method. Then the child operation method returns a partial view, return Partialview ("_leftsidemenu", viewModels);

Html.action () and html.renderaction () are more flexible than html.partial () and Html. RenderPartial () is high, and processing can be handled in the invoked action.

and Html.renderaction () and Html. Action in ASP. NET MVC can be used across controllers to facilitate the reuse of code in the same project. Html. RenderPartial () and html.partial () can only be exploited if the model type is the same

Instance:

In the view list.cshtml of the home Controller's list method:

Div class= "Row" >
<div class= "Col-md-3" >
@* embodies the advantages of a partial view equivalent to the benefits of user control sharing. It can be used by the home controller as well as the article controller, but the host controller must provide the corresponding parameters or viewbag dynamic parameters. Because the Html.renderaction method is used, all that is shared is actually an action method, but the actual effect is a shared view. *@
@{html.renderaction ("Leftsidemenu", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Returns a navigation menu panel that passes a number of arguments through the linked RouteValue. -
@{html.renderaction ("Clickrangeoncategory", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Return a click on this column--


</div>

How to operate in the home controller:

[Childactiononly]

Public ActionResult Leftsidemenu (string Activecategoryid)
{
var categorys = _categoryservice.findall (). Where (c = c.navbarisshow). (c = c.priororder). ToList ();
List<categoryleftsideviewmodel> viewModels =new list<categoryleftsideviewmodel> ();
foreach (var item in Categorys)
{
int _categorycount = _articleservice.findall (). Where (A = A.categoryid = = Item. CategoryID). Count ();
Viewmodels.add (new Categoryleftsideviewmodel {Categoryid=item. Categoryid,categoryname=item. CategoryName, Active= (item. CategoryID ==activecategoryid), articlecount=_categorycount});


}

Return Partialview ("_leftsidemenu", viewModels);

}

[Childactiononly]
Public ActionResult clickrangeoncategory (string Activecategoryid)
{
var articles = _articleservice.findall (). Where (A = = A.category.categoryid = = Activecategoryid);
if (articles. Count () >= 5)
{
Articles = articles. Take (5);
}

Return Partialview ("_clickrangeoncategory", articles. OrderByDescending (A = A.clickcount). ThenByDescending (a =>a.posttime). ToList ());

}

Used in the listarticle.cshtml view in the article controller,

<div class= "Row" >
<div class= "Col-md-3" >
@* embodies the advantages of a partial view equivalent to the benefits of user control sharing. It can be used by the home controller as well as the article controller, but the host controller must provide the corresponding parameters or viewbag dynamic parameters. Because the Html.renderaction method is used, all that is shared is actually an action method, but the actual effect is a shared view. *@
@{html.renderaction ("Leftsidemenu", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Returns a navigation menu panel that passes a number of arguments through the linked RouteValue. -
@{html.renderaction ("Clickrangeoncategory", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Return a click on this column--


</div>

Due to the use of @html. Renderaction (), so there is no need to define an action method in the article controller, just recall the Leftsidemenu, clickrangeoncategory method of the home controller. You can get the same partial view across different views of the controller.

Good practice of the dry principle. (Don't repeat yourself).

Html.partialview (), HTML. Renderpartial,html.action.html.renderaction Auxiliary method

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.