Select only one Radiobutton with Razor

Asked

Viewed 1,517 times

3

I have 2 Radio Buttons, and I would like when I mark one, the other to cancel.

Follow the image of what is occurring:

Code:

@Html.RadioButton("teste", "teste", true) Teste
@Html.RadioButton("concessionaria", "concessionaria", false) Concessionária
  • You can use attr('checked', true) true or false in case.

1 answer

2


Put it like this:

@Html.RadioButton("teste", "teste", true)
@Html.RadioButton("teste", "concessionaria")      

Because the input type radio must bear the same name, the difference being value.

Example with form:

<form action="/Estudo/Post" method="post" enctype="multipart/form-data">
        @Html.RadioButton("teste", "teste", true)
        @Html.RadioButton("teste", "concessionaria")
        <button>Enviar</button>
</form> 

Rendered Html:

<form action="/Estudo/Post" method="post" enctype="multipart/form-data">
        <input checked="checked" id="teste" name="teste" type="radio" value="teste" />
        <input id="teste" name="teste" type="radio" value="concessionaria" />
        <button>Enviar</button>
</form>

Note that the two have the same name with different value that can be recovered by the method Post.

Recovering by the Method Post

public ActionResult Post(string teste)
{
      return View();
}

Debug:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.