Postback on RadioButtonFor in MVC

别来无恙 提交于 2019-12-18 05:21:13

问题


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

the aspx page is shown below.

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. 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?


回答1:


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

and then in a separate javascript file:

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



回答2:


this is one way of doing post back with radio button:

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


来源:https://stackoverflow.com/questions/4977655/postback-on-radiobuttonfor-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!