【发布时间】: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