【问题标题】:How to Load Partial Views on Radio Button selection?如何加载单选按钮选择的部分视图?
【发布时间】:2014-06-03 12:08:43
【问题描述】:

我正在尝试根据单选按钮选择在单个 div 中加载不同的部分视图。当用户单击应显示部分视图的个人按钮时,如果单击业务则应显示业务部分视图

我的查看页面代码是

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

我的控制器代码是

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

        }

谁能帮我找出错误。当我运行此代码时,它直接显示 BusinessPartialName。我无法在输出中看到父视图单选按钮。如果不选择单选按钮,它会直接在我的页面上加载第二个部分视图。

【问题讨论】:

  • 你遇到了什么问题?
  • 当我运行此代码时,它直接显示 BusinessPartialName。我无法在输出中看到父视图单选按钮。如果不选择单选按钮,它会直接在我的页面上加载第二个部分视图。
  • 让我们试试下面会起作用的
  • 让你的 else 成为else if(RegesterHere == "Business")。 RegesterHere 有默认值吗?要尝试的另一件事是使您的模型中的 RegesterHere 可以为空,即public string RegesterHere? {get;set;}。还有一件小事,请纠正拼写错误。应该是RegisterHere

标签: c# ajax asp.net-mvc-4 razor partial-views


【解决方案1】:

让我们试试下面的 AJAX 调用。

 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);
                    });


                   }

【讨论】:

  • 你必须检查你的单选按钮是否被选中。否则会调用 Ajax 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多