I try to describe my scenario.
I have a view that contains two radio buttons and depending on which one is selected, a partial or another view is displayed.
@Html.RadioButtonFor(model => model.TipoVP, new String("VP_A".ToCharArray()), new { @checked = "checked" })
<span class="custom-control-label">Vista parcial A</span>
@Html.RadioButtonFor(model => model.TipoVP, new String("VP_B".ToCharArray()))
<span class="custom-control-label">Vista parcial B</span>
As you can see, when you enter, by default partial view A is shown because you have defined the cheked to true.
The partial views are shown in this div:
<div id="vistaparcial">
@Html.Action("GetVistaParcial", "miControlador")
</div>
I also have a javascript code to show / hide the partial views depending on the radio button that is selected.
$("[name=TipoVP]").on('change', function () {
var $rb = $(this);
alert($rb.val());
$.ajax({
url: '/miControlador/GetVistaParcial/',
data: { tipov: $rb.val() },
success: function (respuesta) {
$("#vistaparcial").html(respuesta);
}
});
});
The action method is like this:
public ActionResult GetVistaParcial(string tipov)
{
if (tipov == null || tipov == "VP_A")
return PartialView("_PartialView_A");
else
return PartialView("_PartialView_B");
}
}
So far in principle everything seems to work fine, when loading the view the partial view A appears first, when I press the radio buttons it is shown and hidden correctly, but ... If I have selected the partial view B and I do a POST to send the data to the controller, to the action method that has to process the data I get the value of the selected radio button (VP_B), but then in the GetVistaParcial action, the value that reaches the parameter is null, with that shows me the partial view that does not touch.
How can I do so that when I make a POST, I keep the value of the selected radio button?
I add this information to respond to Leandro's comments and provide more information to the initial question.
My method of action that should process the data would be like this:
public ActionResult Index(miViewModel vm)
{
/* Aquí en vm.TipoVP tengo VP_A o VP_B según la selección */
//Realizar acciones
/* Devuelvo el modelo a la vista */
return View(vm);
}
Once the return is executed, in the view, I have the line:
@Html.Action("GetVistaParcial", "miControlador", new { tipov = Model.TipoVP } )
The problem is that I do not know how to reference the content of Model.TipoVP to assign it to typev in the HtmlAction ().