简体   繁体   English

在MVC中回传RadioButtonFor

[英]Postback on RadioButtonFor in MVC

I have 2 radio buttons in my mvc webpage. 我的mvc网页上有2个单选按钮。

 <% 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
   <% } %>

the aspx page is shown below. aspx页面如下所示。

I also have button in my page.and if i clicked on that button,then there is a postback occures. 我的页面也有按钮。如果我点击该按钮,则会发生回发。

I need another postback if i changed radiobutton selection. 如果我更改了radiobutton选择,我需要另一个回发。 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? 我怎样才能实现这一目标?

这是使用单选按钮进行回发的一种方式:

<label>@Html.RadioButton("Buyer", "Buyer", new { onchange = "this.form.submit();" })Buyer</label>

You will need to use javascript for this. 你需要使用javascript。 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: 因此,如果您使用的是jquery,则可以订阅.change()事件并提交表单:

<% 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" }) %>
<% } %>

and then in a separate javascript file: 然后在一个单独的JavaScript文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM