开发者

How to toggle between two views using a radio button in a MVC3 view?

I have a page that shows a report, and based on a routing parameter it will return a view that displays the data grouped by category or grouped by priority.

I want to add two Radio buttons to the report page that will let the user toggle between the category and priority view when they click on the corresponding radio button.

开发者_开发技巧

But I don't know how to create a form where clicking on either of the radio buttons submits a post request that passes a model object (has the category priority switch, and other values) back to a controller action.


The only way I know of to make selecting a radio button do a post is to use JavaScript:

document.getElementById('your-radio-button').onchange = function() {
    document.getElementById('your-form').submit();
};

Here's the way you could do it in jQuery:

$('#your-radio-button').change(function() { 
    $('#your-form').submit();
});


I haven't had the time to try it, but with jQuery, you should be able to write something like this:

<script type="text/javascript">
        $(document).ready(function() {
         $("#myform input[type=radio]").click(function(){
            $(this).closest("form").submit();
         })
        })       
    </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜