回发上RadioButtonFor在MVC [英] Postback on RadioButtonFor in MVC

查看:101
本文介绍了回发上RadioButtonFor在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个单选按钮在我的MVC的网页。

I have 2 radio buttons in my mvc webpage.

 <% using (Html.BeginForm("Search", "Search"))
   { %>
  // some html codes
     <%= Html.RadioButtonFor(m => m.Agents, "A", new { Checked = "checked" })%> //radio button 1                     
      <%= Html.RadioButtonFor(m=> m.Agents,"AG") %>  //radio button 2                      
  // some html codes
   <% } %>

aspx页面如下图所示。

the aspx page is shown below.

我也有我的page.and按钮,如果我上单击按钮,再有就是回发occures。

I also have button in my page.and if i clicked on that button,then there is a postback occures.

我需要另一个回发,如果我改变了单选按钮选择。我怎样才能让上回发event.But没有回传,如果我改变了单选按钮的选择。我怎样才能做到这一点?

I need another postback if i changed radiobutton selection. How can i make postback on that event.But there is no postback if i changed the selection of radio buttons. How can i achieve this?

推荐答案

您将需要使用JavaScript这一点。有当用户改变一个单选按钮的选择没有出现服务器端事件。所以,如果你使用jQuery,你可以认购 .change() 事件并提交表单:

You will need to use javascript for this. There is no server side event occurring when the user changes the selection of a radio button. So if you are using jquery you could subscribe for the .change() event and submit the form:

<% using (Html.BeginForm("Search", "Search", FormMethod.Post, new { id = "myform" })) { %>
    <%= Html.RadioButtonFor(m => m.Agents, "A", new { @checked = "checked", @class = "radio" }) %>
    <%= Html.RadioButtonFor(m => m.Agents, "AG", new { @class = "radio" }) %>
<% } %>

,然后在一个单独的JavaScript文件:

and then in a separate javascript file:

$(function() {
    $('#myform .radio').change(function() {
        $('#myform').submit();
    });
});

这篇关于回发上RadioButtonFor在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆