Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was redirecting my View to Controller in .Net.But when i write
@Url.Action("ActionMethod","Controller")
it redirected the page successfully,and when i checked my page view source it was generating url to my controller.But when i accidentally typed
@Html.Action("ActionMethod","Controller")
it does not redirected to my controller,and when i checked view source of my page it was only generating the name of my controller.So are they different?is so,where to use which one?Please help.Thanks for your help.

What I have tried:

@Url.Action("ActionMethod","Controller")
@Html.Action("ActionMethod","Controller")
Posted
Updated 14-Mar-16 3:03am

1 solution

@Url.Action()

It generates a URL as per provided action name, contrillername, route object.
It contains 8 overload methods.
Razor
@Url.Action("ActionMethod","Controller")


@Html.Action()

It calls child action in a controller and return Html string as result.
It contains 6 overload methods.
Razor
@Html.Action("ActionMethod","Controller")

Controller action:
C#
public class MyController
{
	[ChildActionOnly]
	public ActionResult ActionMethod() 
	{
		var menu = GetMenuFromSomewhere();
		  return PartialView(menu);
	}
}

Use @Url.Action() when you are redirecting any page, you can use that in
Razor
<a href="@Url.Action("Action")">click</a>


In other hand you need to use @Html.Action() when you need result as HTML string. You can bind partial in your view page.
 
Share this answer
 
v5
Comments
vaibhav1800 6-Dec-18 8:23am    
Any Scenario where you prefer to use Url.Action over Html.Action ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900