Here lets explore the difference between all these four html helpers which look similar.
First lets jump start with example later we can see the individual theory and purpose.
To be simple enough and concentrate on the target lets create a simple web application with MVC empty template.
Create Home controller having two action methods Index and Test.

public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Test()
{
return View();
}
}

Now Add the Views Index and Test.
Now in Index View if we call the @Html.Action(),@Html.RenderAction,@Html.partial,@Html.RenderPartial all methods execute without throwing any exceptions.

@{Html.RenderAction("Test");}
@Html.Action("Test")
@Html.Partial("Test")
@{Html.RenderPartial("Test");}

But now if we remove the test action method from the Controller and execute then now @Html.RenderAction and @Html.Action() will throw exception because they need the method to be available to execute.
But the @Html.Partial and @Html.RenderPartial will execute since they will find the View named Test and execute the View though it may not be a partial View.These methods do not require the method to be present in the controller.

So the underlying gist is that Parial and RenderPartial are used to render partial Views independent of the methods of controller while Action and RenderAction will invoke the childAction methods.
Though Action and RenderAction’s purpose is to work with childActions they can even work on main actions too.

On the same lines Render versions of helper methods write the output directly to the response Stream having returning void while counterparts return HtmlStrings.

 

Advertisements