简体   繁体   English

将@ Html.ActionLink转换为@ Url.Action

[英]Converting @Html.ActionLink to @Url.Action

I'm trying to convert this: 我正在尝试将其转换为:

@Html.ActionLink("Register", "Register", "Account", 
  routeValues: null, htmlAttributes: new { id = "registerLink" })

To this 对此

<input type="button" title="Register" 
  onclick="location.href='@Url.Action("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" }

I keep erring out on htmlAttributes. 我一直在htmlAttributes上犯错。 I'm trying to make the first link into a button, but keep the same attributes. 我试图使第一个链接成为按钮,但保持相同的属性。 Any advise? 有什么建议吗?

Html attributes has to be added into html :) like: HTML属性必须添加到html :)中,例如:

<input type="button" title="Register" id="registerLink"
  onclick="location.href='@Url.Action("Register", "Account")'" />
<input type="button" title="Register" id="registerLink"
  onclick="window.location='@Url.Action("Register", "Account")'" />

It will work, i have on tried the other ans. 它将起作用,我已经尝试了其他ans。 but this can one option. 但这可以是一种选择。

Answer 回答

The @Url.Action has neither a linkText nor an htmlAttributes parameter. @Url.Action既没有linkText也没有htmlAttributes参数。 What you can do instead, is use the HTML id and value attributes directly. 您可以做的是直接使用HTML idvalue属性。

Example

Here's a working fiddle . 这是工作中的小提琴

In the below example, I've done just that and have used named arguments to illustrate the difference between @Url.Action and @Html.ActionLink . 在下面的示例中,我就这样做了,并使用了命名参数来说明@Url.Action@Html.ActionLink之间的区别。

LinkExtensions.ActionLink Method LinkExtensions.ActionLink方法

    @Html.ActionLink(
        linkText: "Register", 
        actionName: "Register", 
        controllerName: "Account",
        routeValues: null, 
        htmlAttributes: new { id = "registerLink" })

Equivalent UrlHelper.Action Method 等效的UrlHelper.Action方法

    <input 
        id="registerLink"
        type="button" 
        title="Register" 
        value="Register"
        onclick="location.href='@Url.Action(
            actionName: "Register", 
            controllerName: "Account", 
            routeValues: null)'" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM