Question

Après avoir bricolé pour résoudre [ce] [1] problème, je pense que le cœur du problème est le suivant:

Lorsque vous utilisez l'assistant html.RadioButton () html avec un champ Enum as value, vous ne pouvez choisir votre option qu'une seule fois. Après avoir republié la page, les assistants ignorent la valeur définie dans l'appel et définissent tous les boutons radio sur la même valeur, c'est-à-dire la valeur que vous avez sélectionnée dans le message précédent. Est-ce que je fais quelque chose de mal?

Exemple (observez la valeur des boutons)

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

Générera

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

Et va générer la deuxième fois:

<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>
Était-ce utile?

La solution

Cela est dû à un bogue dans le code bêta ASP.NET MVC. J'ai écrit une explication complète du problème sur le forum asp.net MVC. Consultez ce lien

.

Autres conseils

Au cas où quelqu'un se soucierait de la situation, un travail rapide et sale en prévision de la prochaine mise à jour du cadre est nécessaire. Cela ne fait que remplacer la valeur par votre valeur. Ce n’est pas unitaire testé, ni garanti, ni rien.

Placez-le dans votre bibliothèque de classe HtmlHelper ou à l’endroit où vous placez des extensions HtmlHelper. ajoutez les utilisations suivantes:

  • 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;
    }
    

Je sais que cela peut être amélioré et ainsi de suite, mais j’espère vraiment que cela sera réparé bientôt de toute façon. Si vous avez envie d'améliorer, c'est un wiki de la communauté, alors allez-y

Une chose à vérifier. Utilisez-vous le modèle mis à jour pour rendre votre vue? C'est à dire. les mêmes données de modèle qui ont été mises à jour à partir du message ont-elles été transmises à la vue lors de la deuxième affichage?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top