[英]How to get the value of a radio button in razor to pass it as a parameter to an action?
在视图中,我有两个单选按钮:
@Html.RadioButtonFor(model => model.TipoVP, new String("VP_A".ToCharArray()), new { @checked = "checked" })
<span class="custom-control-label">Partial View A</span>
@Html.RadioButtonFor(model => model.TipoVP, new String("VP_B".ToCharArray()))
<span class="custom-control-label">Partial View B</span>
控制器的动作如下:
public ActionResult GetVistaParcial(string tipov)
{
if (tipov == null || tipov == "VP_A")
return PartialView("_PartialView_A");
else
return PartialView("_PartialView_B");
}
}
然后调用控制器动作以根据所选的单选按钮加载局部视图或另一个视图
@Html.Action("GetVistaParcial", "miControlador", new { tipov = "the value of checked RadioButton" } )
单选按钮的change事件的脚本为:
$("[name=TipoVP]").on('change', function () {
var $rb = $(this);
$.ajax({
url: '/miControlador/GetVistaParcial/',
data: { tipov: $rb.val() },
success: function (respuesta) {
$("#vistaparcial").html(respuesta);
}
});
});
如何将选中的单选按钮的值作为参数传递给操作?
在执行GET和POST时必须工作
我想知道您是否要转到另一页。
@using (Html.BeginForm("GetVistaParcial", "Home"))
{
@Html.RadioButtonFor(model => model.TipoVP, new
string("VP_A".ToCharArray()), new { @checked = "checked" })
<span class="custom-control-label">Partial View A</span>
@Html.RadioButtonFor(model => model.TipoVP, new
String("VP_B".ToCharArray()))
<span class="custom-control-label">Partial View B</span>
<input type="submit" value="submit" />
}
并且您不需要调用此:
@Html.Action("GetVistaParcial", "miControlador", new { tipov = "the value of checked RadioButton" } )
您的操作应收到以下模型:
public IActionResult GetVistaParcial(YourModel model)
{
return View();
}
修改以下行数据:{tipov:$ rb.val()}为
data: JSON.stringify({ tipov: $rb.val() }),
将数据发送到Web服务器时,数据必须是字符串,并且JSON.stringify方法将JavaScript对象转换为字符串。
感谢名单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.