Pergunta

Here is my code:

_testPartial.cshtml(Partial view)

 @Html.RadioButtonFor(model => model.CardType, "G", new { @checked = @Model.IsGold})
 @Html.RadioButtonFor(model => model.CardType, "P", new { @checked = @Model.IsPlatinum})
 @Html.RadioButtonFor(model => model.CardType, "B", new { @checked = @Model.IsBusiness})

Controller

public PartialViewResult ShowCardtype() 
{ 
    Model cardType= new Model {
    IsGold=true,
    IsPlatinum=false,
    IsBusiness=false};

    return PartialView("_testPartial",cardType);
}

The expected behavior is, the GOLD radio button option should have been selected in the form. But it always selects the last option. I tried returning "checked" string, but no luck. Appreciate any help.

Foi útil?

Solução

the second option is the selected option, you don't need the new part.

@Html.RadioButtonFor(model => model.CardType, "G") 
@Html.RadioButtonFor(model => model.CardType, "P") 
@Html.RadioButtonFor(model => model.CardType, "B")

when you load the view set model.CardType to G, P, or B and it will select the correct radio button

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top