Postback on RadioButtonFor in MVC

前端 未结 2 1044
囚心锁ツ
囚心锁ツ 2020-12-18 14:03

I have 2 radio buttons in my mvc webpage.

 <% using (Html.BeginForm(\"Search\", \"Search\"))
   { %>
  // some html codes
     <%= Html.RadioButtonF         


        
相关标签:
2条回答
  • 2020-12-18 14:39

    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();
        });
    });
    
    0 讨论(0)
  • 2020-12-18 14:50

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

    <label>@Html.RadioButton("Buyer", "Buyer", new { onchange = "this.form.submit();" })Buyer</label>
    
    0 讨论(0)
提交回复
热议问题