我,爲什麼這個代碼很迷茫爲什麼Html.ActionLink渲染
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
結果在這個環節 「長度= 4?」:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
的hidefocus
部分是我旨在實現,但?Length=4
從哪裏來?
我,爲什麼這個代碼很迷茫爲什麼Html.ActionLink渲染
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
結果在這個環節 「長度= 4?」:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
的hidefocus
部分是我旨在實現,但?Length=4
從哪裏來?
Length = 4來自嘗試序列化字符串對象。你的代碼運行此ActionLink
方法:
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
這需要一個string
對象「家庭」爲routeValues,這對於公衆性的MVC水暖搜索他們變成路徑的值。在string
對象的情況下,唯一的公共屬性是Length
,並且由於不會有使用Length參數定義的路由,因此它將屬性名稱和值附加爲查詢字符串參數。您可能會發現,如果您從不在HomeController
的頁面運行此操作,它會拋出關於缺少About
操作方法的錯誤。請嘗試使用以下:
Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })
在下面看@ jesse的答案,它有一個(恕我直言)更好的解決方案... – THBBFT
@Blah_Blah這些天我不使用這個,更喜歡寫我自己的標籤,只使用Url.Action(「Action」 ,「Controller」) – roryf
@ roryf無後顧之憂......這被用在代碼審查中作爲最佳實踐,他們沒有檢查日期。真的應該有一種方法來過濾日期的計算器... – THBBFT
ActionLink的參數不正確,它試圖使用「Home」值作爲路由值,而不是匿名類型。
我相信你只需要添加new { }
或null
作爲最後一個參數。
編輯:只是重新閱讀這篇文章,並意識到你可能想要指定null作爲第二個參數,而不是最後一個。
恕我直言,這是SOOOO嚇人的煩人。每天發生10次。 – Jabe
告訴我這件事。 – 2009-05-05 10:47:53
順便說一句,他們應該刪除一種方法,因爲它不可能使用它! –
我解決了,這是被匿名聲明(new {}
)之前添加空的第四個參數的方式,以便它使用以下方法重載:(LINKTEXT,actionName,controllerName,routeValues ,htmlAttributes):
Html.ActionLink("About", "About", "Home", null, new { hidefocus = "hidefocus" })
這是最好的答案,並且應該被標記爲答案。 – MoXplod
同樣適用於我,謝謝 –
如果您沒有連接區域,這是最好的答案。如果您需要指定鏈接的區域,則需要使用'controller'和'area'指定routeValues,以防止URL中顯示Length屬性。 –
只是刪除 「家」 的控制器(名稱),這樣的代碼將是:
Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })
你忘了添加HTMLAttributes parm。
這將工作沒有任何改變:
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)
謝謝Jesse Rose,它幫助了! –
+1這個實際上適合我!謝謝! –
這是什麼爲我工作 –
正如喬納森·沃特尼在評論中指出,這也無二
Html.BeginForm()
方法。在我的情況下,我是在一個Create.cshtml靶向相應控制器的發佈請求+創建動作,不得不
using (Html.BeginForm("Create")) {
@Html.AntiForgeryToken()
...
}
其添加查詢字符串「?長度= 6」到表單動作當呈現。通過roryf暗示的批准的答案,實現「創造」的字符串長度爲6,我最後,除去明確的操作規範解決了這個:
using (Html.BeginForm()) {
@Html.AntiForgeryToken()
...
}
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" }, new { })
這將需要過載: 串LINKTEXT,串actionName ,串controllerName,對象routeValues,對象htmlAttributes
隨着屬性名稱:
@Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)
請使用權重載甲基具有五(5)個參數。例如:
@using (@Ajax.BeginForm("Register", "Account", null,
new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "OnSuccess",
OnFailure = "OnFailure",
OnBegin = "OnBegin",
OnComplete = "OnComplete"
}, new { @class = "form-login" }))
添加** null **控制器名稱後有助於我的情況。 –
這與@ Jesse的答案實際上是一樣的答案,它包含了Ajax版本的附加獎勵,這不是OP所要求的,但它與相同的問題有關。 – gcoulby
此行爲也與Html.BeginForm()方法一起出現。 –
這一直讓我在一天中的大部分時間都精神失常。我大大鬆了一口氣。 – 5arx