Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello


<pre><form method="post" asp-controller="Jobs">

        <div class="row">
            @for (int i = 0; i < Model.Questions.Count; i++)
            {
                <div class="col-12">
                    <p class="fw-bold">
                        @Model.Questions[i].Question
                    </p>
                    @for (int j = 0; j < @Model.Questions[i].Answers.Count; j++)
                    {
                        <div class="form-check">
                            <input type="radio" class="form-check-input" asp-for="@Model.Questions[i].SelectedAnswerId" id="@Model.Questions[i].Answers[j].Id" name=" @Model.Questions[i].Id" onchange="filldata()" value="@Model.Questions[i].Answers[j].Id">
                            <input value="55" asp-for="@Model.Questions[i].SelectedAnswerId" hidden />
                            <label class="form-check-label" for="@Model.Questions[i].Answers[j].Id">
                                @Model.Questions[i].Answers[j].Answer
                            </label>
                        </div>
                    }

                </div>
            }
            <div class="col-12">
                <div class="d-flex justify-content-center">
                    <button type="submit" asp-action="SaveExam" class="btn btn-primary px-4 py-2 fw-bold">Submit</button>
                </div>
            </div>


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

<script type='text/javascript'>

   function checkButton() {
        var getSelectedValue = document.querySelector(
            'input[name="@Model.Questions[i].Answers[j].Id"]:checked');

        if (getSelectedValue != null) {
            document.getElementById("disp").innerHTML
                = getSelectedValue.value
                + " is selected";
        }
        else {
            document.getElementById("error").innerHTML
                = "*You have not selected any ";
        }
    }
  

</script>



I have this code and I want to get the value of selected radio button to send it to the controller and I find Solution to get it Via JS but my problem is the id inside for loop so it's giving me an error , is there any other way to save the selected list or how to get the the Name of the input Radio

What I have tried:

Update

<pre>  <form method="post" asp-controller="Jobs">

        <div class="row">
            @for (int i = 0; i < Model.Questions.Count; i++)
            {
            <div class="col-12">
                <p class="fw-bold">
                    @Model.Questions[i].Question
                </p>

                <h3 id="@('d' + i)" style="color:green"> </h3>
                <h4 id="@('E' + i)" style="color:red"> </h4>

                @for (int j = 0; j < @Model.Questions[i].Answers.Count; j++)
                {
                    <div class="form-check">
                        <input type="radio" class="form-check-input"
                               asp-for="@Model.Questions[i].SelectedAnswerId"
                               id="@('A' + Model.Questions[i].Answers[j].Id)"
                               name="@('D' + Model.Questions[i].Id)" onchange="filldata(this.value)"
                               value="@Model.Questions[i].Answers[j].Id" />

                        <input id="@('B' + Model.Questions[i].SelectedAnswerId)" asp-for="@Model.Questions[i].SelectedAnswerId"  hidden />
                        <label class="form-check-label" for="@Model.Questions[i].Answers[j].Id">
                            @Model.Questions[i].Answers[j].Answer
                        </label>
                    </div>
                    <script type='text/javascript'>
                     
                              function filldata(val) {

                            document.getElementById("@('B' + Model.Questions[i].SelectedAnswerId)").value = val
                        }

                    </script>
                }

            </div>

             

            }
            <div class="col-12">
                <div class="d-flex justify-content-center">
                    <button type="button" onclick=" checkButton()"> checkButton </button>
                    <button type="submit" asp-action="SaveExam" class="btn btn-primary px-4 py-2 fw-bold">Submit</button>
                </div>
            </div>
        </div>
    </form>


I found a way to get the id , now Filldata Function Save only the last Item
Posted
Updated 5-Oct-22 22:28pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900