5

I'm a newbie to all this ASP.NET MVC stuff, and I was making some tests for my project. I wanted to ask how is it possible to introduce a javascript function call from the html.radiobutton function. For example, how would you declare this:

<input type="radio" name = "Kingdom" value = "All" onclick="GetSelectedItem(this);" checked ="checked" />

with html.radiobutton. I've been looking for some documentation, but I don't really get to understand, I guess it has something to do with the html object attributes, but don't really know the syntax and I haven't found any example.

Thank you all in advance :) vikitor

1 Answer 1

11

Define the attributes as an anonymous object.

 <%= Html.Radiobutton( "Kingdom",
                       "All",
                       true,
                       new { onclick = "GetSelectedItem(this);" } ) %>

or better yet, apply the handler unobtrusively with javascript (ex. uses jQuery).

 <%= Html.RadioButton( "Kingdom", "All", true ) %>

 <script type="text/javascript">
     $(function() {
         $('input[name=Kingdom]').click( function() {
             GetSelectedItem(this);  // or just put the code inline here
         });
     });
 </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I was looking for this for quite a while and didn't find a proper answer to my question. Greetings

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.