Friday 24 July 2015

@HTML.Action and @HTML.RenderAction in MVC

Action and renderAction are similar to Partial & RenderPartial helpers.
Partial helps to view render a portion of view model using view markup in separate file.

Action execute a separate controller action and display the result. Action offer more flexible and reuse because the controller action can build a different model and make use of a separate controller context.

Difference in action and renderaction is the render action writes directly to the response.


Public class Mycontroller : Controller {
Public ActionResult Index()
{
return View();
}
[ChildActionOnly]
Public ActionResult Menu()
{ Var Menu = GetMenuItems();
Return PartialView(menu);
}}

The menu action Build a menu Model and Return Partial View

@model Menu
<ul>
@foreach(var item in Model.MenuItem)
{
<li> @item.text </li>
}</ul>
You need to call action like
<html>
<head>
<title> Index</title>
<body> @html.Action(“menu”);
<b> Welcome </b>
</bod></html>


Menu action is marked with a child action only  attribute. 

Rendering Helpers

No comments:

Post a Comment