c# - Checkbox and Radio button problems with asp.net core -


i trying develop online examination tool work. have class defines exam contains list of questions, class question contains list of answers, , class answer. have view model pulls out list of questions , passes view array of ints hold value of answer each question. in view, loop through questions, , in each question loop through each possible answer. multiple choice, easy. use:

<input asp-for="selectedanswer[q]" type="radio" value="@a" />@model.questions[q].answers[a].answertext 

this works great, passes selectedanswer int array the controller on submit , can whatever want it. problem comes down if question requires checkboxlist multiple right answers. cant use same int array post answer, hought creating second bool array each of checkboxes in question , send back. works 1 question each new question overrides bool array on postback.

what trying figure out how have different types of questions can displayed on single page, , sent controller when submit button pressed processing.

maybe need better view model? not sure go next this. can provide of code required, didnt want fill entire page code.

my view:

@model dsca_exams.models.examviewmodels.examviewmodel <form asp-action="exam_results">     <div>         @for (int q = 0; q < model.questions.count(); q++)         {             <div>                 <hr />                 <h4>@model.questions[q].questiontext</h4>                 <ul>                     @for (int = 0; < model.questions[q].answers.count(); a++)                     {                         <li>                             @if (model.questions[q].questiontype == 0)                             {                                 <input asp-for="selectedanswer[q]" type="radio" value="@a" />@model.questions[q].answers[a].answertext                             }                             else                             {                                 <input asp-for="@model.isselected[a]"  />@model.questions[q].answers[a].answertext                             }                         </li>                     }                 </ul>             </div>         }         <hr />         <input type="submit" value="grade test" class="btn btn-default" />     </div> </form> 

use view models , preserve object graph. example:

public class questionviewmodel {     public string questiontext { get; set; }     public list<answerviewmodel> answers { get; set; }     public list<int> selectedanswers { get; set; }     ... } 

then, in view:

@for (var = 0; < model.questions.count; i++) {     <p>@model.questions[i].questiontext</p>      @for (var j = 0; j < model.questions[i].answers.count; j++)     {         <label>             <input asp-for="questions[i].selectedanswers" type="radio" value="@model.questions[i].answers[j].id" />             @model.questions[i].answers[j].answertext         </label>     } } 

then, when post, modelbinder able preserve object graph , can inspect selectanswers property of each question.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -