0

I want to send the value from JS function to HTML Helper.

In other word, Fire this helper when the button is clicked.

 function ButtonClick(x) {                   
  @Html.loadSubMenu(4);
}

<a class="Tab" [email protected]> 
<img src="~/Content/images/b1.png" id="b1" 
 onclick="return ButtonClick(1); bb1();" onmouseover="  
bigImg(this)" onmouseout="  normalImg(this)"> </a> 
1
  • You cant. Html helpers are parsed on the server side, while jquery is client side. Commented Aug 7, 2014 at 5:46

2 Answers 2

2

Html Helper is piece of C# code that will run on server and render html inside the pages. The event that made inside HTML page never able to run the C# code on server.

But wait,....

You can make ajax request anytime on your ActionResult.

So You can call the function like this in your JS that will load sub menu from server.

function Test(){
//call the code here that will fetch html form server.
}
Sign up to request clarification or add additional context in comments.

Comments

2

Short answer: You can't do what you are trying to do.

Longer answer:

The asp, mvc and razor code all runs on the server. After it runs on the server, it is turned into a stream of HTML which is then handed to the client.

At that point, server-side code no longer runs. It is no longer available. The browser knows nothing about it at all.

The only thing you can do is run some client side code, written in JavaScript.

As I look at your code and try to guess the intent, I believe that you are trying to make a set of extra buttons available when the user clicks the navigate button.

One possible alternative is to go ahead and create the buttons using razor, but wrap them in a something like this:

// 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>

1 Comment

Thank you but I needed the value passing to the helper to be dynamic. It is working with me now. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.