Html.radiobutton define todos os valores para o valor selecionado
-
07-07-2019 - |
Pergunta
Depois de mexer para resolver [este] [1] problema, acho que o núcleo do problema é o seguinte:
Quando você usa o HTML.RadioButton () HTML Helper com um campo Enum como valor, você só pode escolher sua opção uma vez. Depois de reposicionar a página, os ajudantes ignorarão o valor definido na chamada e definirá todos os botões de rádio para o mesmo valor, sendo o valor que você selecionou na postagem anterior. Estou fazendo algo errado?
Exemplo (assista ao valor dos botões)
<fieldset>
<legend>Test</legend>
<div>
<label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
Any
</label>
<%=Html.RadioButton("SearchBag.EffectIndicator", "Any" , ViewData.Model.SearchBag.EffectIndicatorIsAny, new { @id = "SearchBag.EffectIndicatorAny" })%>
</div>
<div>
<label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
Solid
</label>
<%=Html.RadioButton("SearchBag.EffectIndicator", "Solid", ViewData.Model.SearchBag.EffectIndicatorIsSolid, new { @id = "SearchBag.EffectIndicatorSolid" })%>
</div>
<div>
<label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
Effect
</label>
<%=Html.RadioButton("SearchBag.EffectIndicator", "Effect", ViewData.Model.SearchBag.EffectIndicatorIsEffect, new { @id = "SearchBag.EffectIndicatorEffect" })%>
</div>
</fieldset>
Irá gerar
<fieldset>
<legend>Effect</legend>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
Any
</label>
<input checked="checked" id="SearchBag.EffectIndicatorAny" name="SearchBag.EffectIndicator" type="radio" value="Any" />
</div>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
Solid
</label>
<input id="SearchBag.EffectIndicatorSolid" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
</div>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
Effect
</label>
<input id="SearchBag.EffectIndicatorEffect" name="SearchBag.EffectIndicator" type="radio" value="Effect" />
</div>
</fieldset>
E gerará a segunda vez:
<fieldset>
<legend>Effect</legend>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorAny" id="EffectIndicatorAnyLabel">
Any
</label>
<input id="SearchBag.EffectIndicatorAny" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
</div>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorSolid" id="EffectIndicatorSolidLabel">
Solid
</label>
<input checked="checked" id="SearchBag.EffectIndicatorSolid" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
</div>
<div class="horizontalRadio">
<label for="SearchBag.EffectIndicatorEffect" id="EffectIndicatorEffectLabel">
Effect
</label>
<input id="SearchBag.EffectIndicatorEffect" name="SearchBag.EffectIndicator" type="radio" value="Solid" />
</div>
</fieldset>
Solução
Isso se deve a um bug no código beta do ASP.NET MVC. Escrevi uma explicação completa da questão no fórum ASP.NET MVC. Consulte isso link
Outras dicas
Caso alguém se importe aqui é um trabalho muito rápido e sujo, antecipando a próxima atualização da estrutura. Ele apenas regexreplas o valor com seu valor. Não é testado em unidade, não é garantido não o que for.
Coloque -o na sua biblioteca de classe HTMLHelper ou onde quer que você coloque extensões HTMLHelper. Adicione as seguintes usuras:
- System.Text.Regularexpressions;
System.web.mvc.html;
/*ToDo: remove when patched in framework*/ public static string MonkeyPatchedRadio(this HtmlHelper htmlHelper, string name, object value, bool isChecked, object htmlAttributes){ string monkeyString = htmlHelper.RadioButton(name, value, isChecked, htmlAttributes); monkeyString = Regex.Replace(monkeyString, "(?<=value=\").*(?=\".*)", value.ToString()); return monkeyString; }
Eu sei que isso pode ser feito melhor e outros enfeites, mas realmente espero que seja corrigido em breve. Se você sentir vontade de melhorar, é wiki da comunidade, então vá em frente
Uma coisa para verificar. Você está usando o modelo atualizado para renderizar sua visão? O IE é os mesmos dados de modelo que foram atualizados da postagem passada para a visualização na segunda vez que é exibida?