如何获取剃须刀中单选按钮的值以将其作为参数传递给操作?

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

单选按钮更改事件的脚本是:

 $("[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" } ) 

你的 Action 应该接收模型:

public IActionResult GetVistaParcial(YourModel model)
{
   return View();
}

修改此行数据:{ tipov: $rb.val() } 为

       data: JSON.stringify({ tipov: $rb.val() }),

向 Web 服务器发送数据时,数据必须是字符串,JSON.stringify 方法将 JavaScript 对象转换为字符串。

谢谢