Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button with html.radiobutton ASP.NET MVC

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

like image 347
vikitor Avatar asked Mar 12 '10 16:03

vikitor


1 Answers

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>
like image 169
tvanfosson Avatar answered Nov 18 '22 19:11

tvanfosson