单选按钮与html.radiobutton ASP.NET MVC

vik*_*tor 5 html asp.net-mvc

我是所有这些ASP.NET MVC的新手,我正在为我的项目做一些测试.我想问一下如何从html.radiobutton函数引入一个javascript函数调用.例如,您如何声明:

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

用html.radiobutton.我一直在寻找一些文档,但我真的不明白,我想这与html对象属性有关,但我不知道语法,我没有找到任何例子.

提前谢谢大家:) vikitor

tva*_*son 11

将属性定义为匿名对象.

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

或者更好的是,使用javascript(例如使用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>
Run Code Online (Sandbox Code Playgroud)