asp.net-mvc ASP.NET Razor 中的 HTML.ActionLink 与 Url.Action

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7709001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:30:45  来源:igfitidea点击:

HTML.ActionLink vs Url.Action in ASP.NET Razor

asp.net-mvcasp.net-mvc-3razor

提问by Pankaj Upadhyay

Is there any difference between HTML.ActionLinkvs Url.Actionor they are just two ways of doing the same thing?

HTML.ActionLinkvs之间有什么区别,Url.Action或者它们只是做同一件事的两种方式?

When should I prefer one over the other?

我什么时候应该更喜欢一个?

回答by Darin Dimitrov

Yes, there is a difference. Html.ActionLinkgenerates an <a href=".."></a>tag whereas Url.Actionreturns only an url.

是,有一点不同。Html.ActionLink生成一个<a href=".."></a>标签而Url.Action只返回一个 url。

For example:

例如:

@Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)

generates:

产生:

<a href="/somecontroller/someaction/123">link text</a>

and Url.Action("someaction", "somecontroller", new { id = "123" })generates:

Url.Action("someaction", "somecontroller", new { id = "123" })生成:

/somecontroller/someaction/123

There is also Html.Actionwhich executes a child controller action.

还有Html.Action执行子控制器操作。

回答by Pranav Labhe

Html.ActionLinkgenerates an <a href=".."></a>tag automatically.

Html.ActionLink<a href=".."></a>自动生成标签。

Url.Actiongenerates only an url.

Url.Action只生成一个 url。

For example:

例如:

@Html.ActionLink("link text", "actionName", "controllerName", new { id = "<id>" }, null)

generates:

产生:

<a href="/controllerName/actionName/<id>">link text</a>

and

@Url.Action("actionName", "controllerName", new { id = "<id>" }) 

generates:

产生:

/controllerName/actionName/<id>

Best plus point which I like is using Url.Action(...)

我喜欢的最佳加点是使用 Url.Action(...)

You are creating anchor tag by your own where you can set your own linked text easily even with some other html tag.

您正在自己创建锚标记,即使使用其他一些 html 标记,您也可以轻松设置自己的链接文本。

<a href="@Url.Action("actionName", "controllerName", new { id = "<id>" })">

   <img src="<ImageUrl>" style"width:<somewidth>;height:<someheight> />

   @Html.DisplayFor(model => model.<SomeModelField>)
</a>

回答by Rohit Singh

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Company", FormMethod.Get))
{
    <p>
        Find by Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
        <input type="button" value="Clear" onclick="location.href='@Url.Action("Index","Company")'"/>
    </p>
}

In the above example you can see that If I specifically need a button to do some action, I have to do it with @Url.Action whereas if I just want a link I will use @Html.ActionLink. The point is when you have to use some element(HTML) with action url is used.

在上面的示例中,您可以看到如果我特别需要一个按钮来执行某些操作,我必须使用@Url.Action 来完成,而如果我只想要一个链接,我将使用@Html.ActionLink。关键是当您必须使用某些元素(HTML)和操作 url 时。

回答by Arsman Ahmad

@HTML.ActionLinkgenerates a HTML anchor tag. While @Url.Actiongenerates a URLfor you. You can easily understand it by;

@HTML.ActionLink生成一个HTML anchor tag. 同时为您@Url.Action生成一个URL。你可以很容易地理解它;

// 1. <a href="/ControllerName/ActionMethod">Item Definition</a>
@HTML.ActionLink("Item Definition", "ActionMethod", "ControllerName")

// 2. /ControllerName/ActionMethod
@Url.Action("ActionMethod", "ControllerName")

// 3. <a href="/ControllerName/ActionMethod">Item Definition</a>
<a href="@Url.Action("ActionMethod", "ControllerName")"> Item Definition</a>

Both of these approaches are different and it totally depends upon your need.

这两种方法都是不同的,这完全取决于您的需要。

回答by Altair

You can easily present Html.ActionLinkas a button by using the appropriate CSS style. For example:

通过使用适当的 CSS 样式,您可以轻松地将Html.ActionLink呈现为按钮。例如:

@Html.ActionLink("Save", "ActionMethod", "Controller", new { @class = "btn btn-primary" })

回答by Aneel Goplani

I used the code below to create a Button and it worked for me.

我使用下面的代码创建了一个按钮,它对我有用。

<input type="button" value="PDF" onclick="location.href='@Url.Action("Export","tblOrder")'"/>