2017-03-03 25 views
0

目前,我有一些文字和模型變量<span>標籤內的按鈕下面的代碼:如何將一個span標籤放置在一個ActionLink中並將其設計爲一個按鈕?

<button class="cart-icon fr btn btn-info" type="button"> 
      Cart <span class="badge cart-count">@Model.CartCount</span> 
    </button> 

enter image description here

我希望能夠給該按鈕鏈接到的索引操作ShoppingCart控制器。

它應該看起來像這樣的事情,但我不是關於如何使用HTML助手來訪問span標籤完全肯定:

@Html.ActionLink("Cart", "Index", "ShoppingCart", new { @Model.CartCount}, new { @class = "cart-icon fr btn btn-info"}) 

我要確保我有持有該按鈕內CartCount的Model屬性。

+3

你不能,至少使用'@ Html.ActionLink()' - 只生成文本內容的''標籤。但是,您可以使用'@ Url.Action()'生成url部分。例如'Cart....' –

+2

或編寫自定義幫手如果需要在項目的多個地方 –

+1

看看這裏的例子圖片鏈接:https://stackoverflow.com/questions/23535704/image-button-in-actionlink-mvc/23537462 #23537462 –

回答

0

這解決它:

<a href="@Url.Action("Index", "ShoppingCart", "")" class="cart-icon fr btn btn-info" type="button"> 
      Cart <span class="badge cart-count">@Model.CartCount</span> 
    </a> 
1
<button class="cart-icon fr btn btn-info" type="button"   
onclick="location.href='<%: Url.Action("Action", "Controller") %>'"> 
     Cart <span class="badge cart-count">@Model.CartCount</span> 
     </button> 
相關問題