在ASP.NET MVC中,我試圖創建一個包含錨標記的鏈接(即,將用戶定向到頁面以及頁面的特定部分)。我嘗試創建的URL應該如下所示:<a href="/category/subcategory/1#section12">Title for a section on the page</a>我的路由設置為以下標準:routes.MapRoute("Default", "{controller}/{action}/{categoryid}"); 我正在使用的動作鏈接語法是:<%foreach (Category parent in ViewData.Model) { %><h3><%=parent.Name %></h3><ul><%foreach (Category child in parent.SubCategories) { %> <li><%=Html.ActionLink<CategoryController>(x => x.Subcategory(parent.ID), child.Name) %></li><%} %></ul><%} %>我的控制器方法如下:public ActionResult Subcategory(int categoryID){ //return itemList return View(itemList);}上面的代碼正確地返回了一個URL,如下所示:<a href="/category/subcategory/1">Title for a section on the page</a>我不知道如何添加#section12部分。“部分”一詞只是我用來拆分頁面部分的約定,而12是子類別的ID,即child.ID。我怎樣才能做到這一點?
3 回答

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
有一些采用片段參數的ActionLink重載。將“ section12”作為片段傳遞將使您獲得所要遵循的行為。
例如,調用LinkExtensions.ActionLink方法(HtmlHelper,字符串,字符串,字符串,字符串,字符串,字符串,字符串,對象,對象):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
- 3 回答
- 0 關注
- 522 瀏覽
添加回答
舉報
0/150
提交
取消