When doing POST I do not correctly recover the value of a radio button

0

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 ().

    
asked by Carlos 20.12.2018 в 15:21
source

1 answer

0

To keep the value of the radio button must be sent as part of the submit to the action that receives the post , then send it again in the render of the view

If in the cshtml you define

@Html.RadioButtonFor(m => m.tipov,"VP_A")
@Html.RadioButtonFor(m => m.tipov,"VP_B")

then in the action you receive it to assign it as part of the model

public class xxModel{
   //resto propiedades
   public string tipov {get;set;}
}

[HttpPost]
public ActionResult Actionpost(xxModel model){
    //aqui codigo

    return(model);
}

You can change the value of the model, but if you return it as you arrive, you send again the selection that is rendered in the view

    
answered by 20.12.2018 в 17:08