简体   繁体   中英

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. 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:

<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. but this can one option.

Answer

The @Url.Action has neither a linkText nor an htmlAttributes parameter. What you can do instead, is use the HTML id and value attributes directly.

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 .

LinkExtensions.ActionLink Method

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

Equivalent UrlHelper.Action Method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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