[英]How to fire HTML helper when the button is clicked using Javascript
我想將值從JS函數發送到HTML Helper。
換句話說,單擊該按鈕時解雇此助手。
function ButtonClick(x) {
@Html.loadSubMenu(4);
}
<a class="Tab" href=@item.NavigateUrl>
<img src="~/Content/images/b1.png" id="b1"
onclick="return ButtonClick(1); bb1();" onmouseover="
bigImg(this)" onmouseout=" normalImg(this)"> </a>
簡短答案:您無法做您想做的事情。
更長的答案:
asp,mvc和razor代碼均在服務器上運行。 它在服務器上運行后,將變成HTML流,然后將其傳遞給客戶端。
那時,服務器端代碼不再運行。 它不再可用。 瀏覽器對此一無所知。
您唯一可以做的就是運行一些用JavaScript編寫的客戶端代碼。
當我查看您的代碼並嘗試猜測其意圖時,我相信您正在嘗試在用戶單擊導航按鈕時使一組額外的按鈕可用。
一種可能的選擇是繼續使用剃刀創建按鈕,然后將它們包裹起來,如下所示:
// Example code -- Not meant for actual production
<div style="display:none" id="extra-nav">
@Html.loadSubMenu(4);
</div>
<img src="~/Content/images/b1.png" id="b1"
onclick="handleNavClick" onmouseover="
bigImg(this)" onmouseout=" normalImg(this)"> </a>
<script>
function handleNavClick() {
document.getElementById("extra-nav").style.display = "block";
}
</script>
Html Helper是一段C#代碼,它將在服務器上運行並在頁面內呈現html。 HTML頁面內發生的事件永遠無法在服務器上運行C#代碼。
可是等等,....
您可以隨時在ActionResult上發出ajax請求。
因此,您可以在JS中調用這樣的函數,該函數將從服務器加載子菜單。
function Test(){
//call the code here that will fetch html form server.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.