@ url.action()和@html.action()之间有什么区别 [英] What is the difference between @url.action() and @html.action()
问题描述
当我写
@ Url.Action(ActionMethod,Controller)视图重定向到控制器 >它成功地重定向了页面,当我检查我的页面查看源时,它正在生成 url到我的控制器。但是当我不小心输入
@ Html.Action(ActionMethod, 控制器)
它没有重定向到我的控制器,当我检查我的页面的查看源时它只生成我的控制器的名称。它们是不同的?是这样,在哪里使用哪个一个?请帮忙。感谢您的帮助。
我尝试过的事情:
@ Url.Action(ActionMethod,Controller)
@ Html.Action(ActionMethod,Controller)
@ Url.Action()
它根据提供的动作名称,contrillername,路由对象生成一个URL。 br />
它包含8个重载方法。
@ Url .Action( ActionMethod, 控制器)
@ Html.Action( )
它在控制器中调用子操作并返回Html字符串。
它包含6个重载方法。
@ Html .Action( < span class =code-string> ActionMethod, Controller)
控制器操作:
public class MyController
{
[ChildActionOnly]
public ActionResult ActionMethod()
{
var menu = GetMenuFromSomewhere();
return PartialView(菜单);
}
}
当您重定向任何页面时使用@ Url.Action(),您可以在
$ b中使用它$ b< a href = @ Url.Action ( 操作) > 点击< / a >
另一方面,当你需要结果为HTML字符串时,你需要使用@ Html.Action()。您可以在视图页面中绑定partial。
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")
@Url.Action()
It generates a URL as per provided action name, contrillername, route object.
It contains 8 overload methods.
@Url.Action("ActionMethod","Controller")
@Html.Action()
It calls child action in a controller and return Html string as result.
It contains 6 overload methods.
@Html.Action("ActionMethod","Controller")
Controller action:
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
<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.
这篇关于@ url.action()和@html.action()之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!