Select only one RadioButton with Razor

3

I have 2 Radio Buttons, and I would like it when I tagged one, the other unchecked.

Here's what's happening:

Code:

@Html.RadioButton("teste", "teste", true) Teste
@Html.RadioButton("concessionaria", "concessionaria", false) Concessionária
    
asked by anonymous 23.06.2014 / 15:15

1 answer

2

Put it like this:

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

Because input type radio must have the same name, its 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> 

Html Rendering

<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>

Notice that the two have the same name with different values that can be retrieved by the Post method.

Retrieving by method Post

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

Debug:

    
23.06.2014 / 15:47