1

I am trying to load different partial views in a single div by depending on radio button selection. when the user clicks on Individual button that partial view should appear, if click on Business then Business Partial view should appear

My view page code is

<script type="text/javascript">
    $(function () {
        $("[name=RegesterHere]").on('change', function () {
            var $radio = $(this);

            $.ajax({
                url: '@Url.Action("GetRegisterForm", "Home")',
                data: RegesterHere = $radio.val(),
                type: 'GET',
                success: function (data) {
                    $("#loadpartial").html(data);
                }
            });
        });
    });
</script>

<h2>GetRegisterForm</h2>
<table>

    <tr>
        <td colspan="1">@Html.LabelFor(m => m.RegesterHere)</td>
        <td colspan="2">@Html.RadioButtonFor(m => m.RegesterHere, "Individual") Individual
                          @Html.RadioButtonFor(m => m.RegesterHere, "Business") Business</td>
    </tr>
</table>

<div id="loadpartial">
</div>

and my controller code is

[HttpGet]
        public PartialViewResult GetRegisterForm(string RegesterHere)
        {
            if (RegesterHere == "Individual")
            {
                return PartialView("IndividualPartialViewName");
            }
            else
            {
                return PartialView("BusinessPartialViewName");
            }

        }

Can someone help me to find the error. When I am running this code its directly showing BusinessPartialName. I am not able to see Parent View Radio Buttons in the output. Without selecting radio button it directly loading second partial view on my page.

user3628878
  • 53
  • 2
  • 12
  • which problem you face ? – MSTdev Jun 03 '14 at 12:15
  • When I am running this code its directly showing BusinessPartialName. I am not able to see Parent View Radio Buttons in the output. Without selecting radio button it directly loading second partial view on my page. – user3628878 Jun 03 '14 at 12:18
  • lets try with below will work – MSTdev Jun 03 '14 at 12:23
  • make your else an `else if(RegesterHere == "Business")`. Does RegesterHere has a default value? another thing to try is to make RegesterHere in you Model nullable i.e `public string RegesterHere? {get;set;}`. and one minor thing, please correct the spelling mistake. it should be `RegisterHere` – Amila Jun 03 '14 at 12:31
  • possible duplicate of [Load Different partial views on radio button click](http://stackoverflow.com/questions/24017314/load-different-partial-views-on-radio-button-click) – J. Steen Jun 13 '14 at 12:08

1 Answers1

2

Lets try with below AJAX Call .

 var radiobuttonval = $radio.val();

 var isselect = $("input:radio[name=RegesterHere]:checked").val();
             if(isselect !="" || isselect !=undefined){
                 $.get("@Url.Action("GetRegisterForm", "Home")", {RegesterHere : radiobuttonval  }, function (data) {
                        $("#loadpartial").html("");
                        $("#loadpartial").html(data);
                    });


                   }
MSTdev
  • 4,507
  • 2
  • 23
  • 40