ASP.NET Razor中的HTML.ActionLink与Url.Action

本文翻译自:HTML.ActionLink vs Url.Action in ASP.NET Razor

Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing? HTML.ActionLinkUrl.Action之间是否有区别,或者它们只是做同一件事的两种方式?

When should I prefer one over the other? 我什么时候比另一个更喜欢?


#1楼

参考:https://stackoom.com/question/wlsJ/ASP-NET-Razor中的HTML-ActionLink与Url-Action


#2楼

<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. 在上面的示例中,您可以看到,如果我特别需要一个按钮来执行某些操作,则必须使用@ Url.Action进行操作,而如果我只想要一个链接,则将使用@ Html.ActionLink。 The point is when you have to use some element(HTML) with action url is used. 关键是当您必须使用某些元素(HTML)和操作网址时。


#3楼

Html.ActionLink generates an <a href=".."></a> tag automatically. Html.ActionLink自动生成一个<a href=".."></a>标记。

Url.Action generates 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>

#4楼

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

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

#5楼

You can easily present Html.ActionLink as a button by using the appropriate CSS style. 您可以使用适当的CSS样式轻松地将Html.ActionLink呈现为按钮。 For example: 例如:

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

#6楼

@HTML.ActionLink generates a HTML anchor tag . @HTML.ActionLink生成HTML anchor tag While @Url.Action generates a URL for you. @Url.Action为您生成一个URL You can easily understand it by; 您可以很容易地理解它;

// 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. 这两种方法都不相同,这完全取决于您的需求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值