Radio button with html.radiobutton ASP.NET MVC

I am new to all this ASP.NET MVC stuff and I did some tests for my project. I wanted to ask how you can introduce a javascript function call from the html.radiobutton function. For example, how would you state this:

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

with html.radiobutton. I was looking for some kind of documentation, but I don’t really understand, I think it has something to do with the attributes of the html object, but I don’t really know the syntax, and I did not find any example.

Thanks to everyone in advance :) vikitor

+5
source share
1 answer

Define attributes as an anonymous object.

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

or better yet, apply the handler unobtrusively with javascript (e.g. 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>
+11

All Articles