Padre7
Padre7

Reputation: 3

mvc radiobuttonfor in editortemplate keyboard navigation does not set model

Context: Model generating some RadioButtonFor groupings as input to answer questions.

What is happening:

Case 1. When mouse click on a radio option the display looks correct. When the [HttpPost] ActionResult(model) for the page is triggered the Model.Answer comes through with the correct value. Which is good and desired.

Case 2. When navigating with the keyboard to the radio group and selecting one with arrow keys the display looks correct. But when the [HttpPost] ActionResult (model) is triggered the Model.Answer value is unchanged from what it was loaded as on page load.

Here is the code that makes the radio group:

@model NexusPWI.ViewModels.Wizard.QuestionModel
@* Dynamically generate and model bind controls for QuestionModel *@
@{
    <div class="row d-contents">
        <div class="form-group">
            <div class="question">
                <div class="col-lg-2 d-contents">
                    <div class="btn-group btn-toggle group-sm d-contents" data-toggle="buttons">
                        <label class="btn QuestionRadio btn-default @(Model.Answer == YesNoNAOptions.Yes ? "active" : "")" for="@Model.Answer">
                            @YesNoNAOptions.Yes.ToString().ToUpper()
                            @Html.RadioButtonFor(Model => Model.Answer, YesNoNAOptions.Yes, new { @onfocus = "radiofocus(event)", @onblur = "radioblur(event)" })
                        </label>
                        <label class="btn QuestionRadio btn-default @(Model.Answer == YesNoNAOptions.No ? "active" : "")" for="@Model.Answer">
                            @YesNoNAOptions.No.ToString().ToUpper()
                            @Html.RadioButtonFor(Model => Model.Answer, YesNoNAOptions.No, new { @onfocus = "radiofocus(event)", @onblur = "radioblur(event)" })
                        </label>
                        @if (!Model.NaInvalid)
                        {
                            <label class="btn QuestionRadio btn-default @(Model.Answer == YesNoNAOptions.NA ? "active" : "")" for="@Model.Answer">
                                N/A
                                @Html.RadioButtonFor(Model => Model.Answer, YesNoNAOptions.NA, new { @onfocus = "radiofocus(event)", @onblur = "radioblur(event)" })
                            </label>
                        }
                    </div>
                </div>
                <div class="col-lg-9 d-contents">
                    <div class="row">
                        <p>
                            <strong>@Model.Question</strong>
                        </p>
                    </div>

                    @Html.HiddenFor(x => Model.Question_IdentityMarker, new { @class = "Question_IdentityMarker" })

                </div>
            </div>
        </div>
    </div>

}

Here is an example of the html that is generated:

<div class="form-group">
    <div class="question">
        <div class="col-lg-2 d-contents">
            <div class="btn-group btn-toggle group-sm d-contents" data-toggle="buttons">
                <label class="btn QuestionRadio btn-default " for="No">
                    YES
                    <input data-val="true" data-val-required="The Answer field is required." id="Questions_0__Answer" name="Questions[0].Answer" onblur="radioblur(event)" onfocus="radiofocus(event)" value="Yes" type="radio">
                </label>
                <label class="btn QuestionRadio btn-default active" for="No">
                    NO
                    <input checked="checked" id="Questions_0__Answer" name="Questions[0].Answer" onblur="radioblur(event)" onfocus="radiofocus(event)" value="No" type="radio">
                </label>
                    <label class="btn QuestionRadio btn-default " for="No">
                        N/A
                        <input id="Questions_0__Answer" name="Questions[0].Answer" onblur="radioblur(event)" onfocus="radiofocus(event)" value="NA" type="radio">
                    </label>
            </div>
        </div>
        <div class="col-lg-9 d-contents">
            <div class="row">
                <p>
                    <strong>Do you charge sales tax?</strong>
                </p>
            </div>

            <input class="Question_IdentityMarker" id="Questions_0__Question_IdentityMarker" name="Questions[0].Question_IdentityMarker" value="CUSTOMERSALESTAX" type="hidden">
         </div>
    </div>
</div>

EDIT Adding onFocus & onBlur at request: onFocus & onBlur are css highlighting for the keyboard navigation to make it more clear for the user where they are in the page.

function radiofocus(event) {
    // Get event object  if using Internet Explorer 
    var e = event || window.event;
     // Check the object for W3C DOM event object, if not use IE event object to update the class of the parent element 
    if (e.target) {
        var addClass = focusClass(e.target.parentNode.parentNode.parentNode.parentNode.parentNode.className, "r");
        e.target.parentNode.parentNode.parentNode.parentNode.parentNode.className = addClass;
    } else {
        var addClass = focusClass(e.srcElement.parentNode.parentNode.parentNode.parentNode.parentNode.className, "r");
        e.srcElement.parentNode.parentNode.parentNode.parentNode.parentNode.className = addClass;
    }
};
function radioblur(event) {
    // Get event object  if using Internet Explorer 
    var e = event || window.event;
    var removeClass = focusClass("", "r").trim();
    // Check the object for W3C DOM event object, if not use IE event object to update the class of the parent element 
    if (e.target) {
        e.target.parentNode.parentNode.parentNode.parentNode.parentNode.className = e.target.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(removeClass, "");
    } else {
        e.srcElement.parentNode.parentNode.parentNode.parentNode.parentNode.className = e.srcElement.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(removeClass, "");
    }
};

Why do the keyboard navigated changes not get back to the controller?

Anything to add to make this clearer?

Side note: For some reason before a value is chosen in the radio group the keyboard tab navigation is stopping for each radio answer for a question.

Upvotes: 0

Views: 62

Answers (0)

Related Questions