簡體   English   中英

將@ Html.ActionLink轉換為@ Url.Action

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

我正在嘗試將其轉換為:

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

對此

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

我一直在htmlAttributes上犯錯。 我試圖使第一個鏈接成為按鈕,但保持相同的屬性。 有什么建議嗎?

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")'" />

它將起作用,我已經嘗試了其他ans。 但這可以是一種選擇。

回答

@Url.Action既沒有linkText也沒有htmlAttributes參數。 您可以做的是直接使用HTML idvalue屬性。

這是工作中的小提琴

在下面的示例中,我就這樣做了,並使用了命名參數來說明@Url.Action@Html.ActionLink之間的區別。

LinkExtensions.ActionLink方法

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

等效的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