AminM
AminM

Reputation: 1824

get selected value of multiple group of radio button asp.net mvc 5

i have single page with several questions,each question should answerd by serveral user,answer are stored in table called QuestionReviews
i use below code for each user to select one option from radio button

foreach (var question in Model.Questions)
     {
       @Html.Hidden("QuestionId", question.QuestionId.ToString())
         <div class="row m-0  bg-silver-light border">
             <div class="col-12 bg-light pt-2 pb-2">
                 <h6>
                     @question.QuestionTitle
                 </h6>
                 <p class="text-secondary small m-0">
                     @question.QuestionBody
                 </p>
             </div>


             <div class="col-12 pt-2 pb-2">
                 <div class="form-check-inline">
                     <label class="form-check-label" for="radio2">
                         <input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio2" value="20">20
                     </label>
                 </div>
                 <div class="form-check-inline">
                     <label class="form-check-label" for="radio3">
                         <input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio3" value="40">40
                     </label>
                 </div>
                 <div class="form-check-inline">
                     <label class="form-check-label" for="radio4">
                         <input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio4" value="60">60
                     </label>
                 </div>
                 <div class="form-check-inline">
                     <label class="form-check-label" for="radio5">
                         <input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio5" value="80">80
                     </label>
                 </div>
                 <div class="form-check-inline">
                     <label class="form-check-label" for="radio6">
                         <input name="@("q"+question.SeminarQuestionId)" type="radio" class="form-check-input" id="radio6" value="100">100
                     </label>
                 </div>
             </div>
         </div>
     }  

and here is my action method:

foreach (string key in Request.Form.Keys)
            {

                if (key == "QuestionId") 
                {
                    int qId = Convert.ToInt32(Request.Form[key]);
                    var q = _dbContext.Questions.FirstOrDefault(x => x.QuestionId == qId);
                    var reviews = _dbContext.QuestionReviews.Where(x => x.QuestionId == qId).ToList();
                    foreach (var review in reviews)
                    {
                        var k = "q" + review.QuestionId;  /// get q1,q2 and ... 
                        review.ReviewPoint =Convert.ToInt32(Request.Form[k]);
                    }

                }
            }

after user click on submit button i should get list of question ids and answer values in controller my problem is user should answer several question at same time and i dont know how to get questionId and selected radio value for each of them in controller

Upvotes: 0

Views: 1851

Answers (1)

acurtis166
acurtis166

Reputation: 106

If you convert your foreach loop within your View to a for loop, you can associate your values by using the same index i for your input names:

@for (int i = 0; i < Model.Questions.Count(); i++)
{
    @Html.HiddenFor(model => model.Questions[i].QuestionId)
    <div class="row m-0  bg-silver-light border">
        <div class="col-12 bg-light pt-2 pb-2">
            <h6>
                @Model.Questions[i].QuestionTitle
            </h6>
            <p class="text-secondary small m-0">
                @Model.Questions[i].QuestionBody
            </p>
        </div>


        <div class="col-12 pt-2 pb-2">
            <div class="form-check-inline">
                <label class="form-check-label" for="radio2">
                    @Html.RadioButtonFor(model => model.Questions[i].Answer, 20, htmlAttributes: new { @class = "form-check-input", @id = "radio2"})
                    20
                </label>
            </div>
            <div class="form-check-inline">
                <label class="form-check-label" for="radio3">
                    @Html.RadioButtonFor(model => model.Questions[i].Answer, 40, htmlAttributes: new { @class = "form-check-input", @id = "radio3" })
                    40
                </label>
            </div>
            <div class="form-check-inline">
                <label class="form-check-label" for="radio4">
                    @Html.RadioButtonFor(model => model.Questions[i].Answer, 60, htmlAttributes: new { @class = "form-check-input", @id = "radio4" })
                    60
                </label>
            </div>
            <div class="form-check-inline">
                <label class="form-check-label" for="radio5">
                    @Html.RadioButtonFor(model => model.Questions[i].Answer, 80, htmlAttributes: new { @class = "form-check-input", @id = "radio5" })
                    80
                </label>
            </div>
            <div class="form-check-inline">
                <label class="form-check-label" for="radio6">
                    @Html.RadioButtonFor(model => model.Questions[i].Answer, 100, htmlAttributes: new { @class = "form-check-input", @id = "radio6" })
                    100
                </label>
            </div>
        </div>
    </div>
} 

And assuming your ViewModel has a property Questions, like so:

public class QuestionReviewViewModel
{
    public List<QuestionModel> Questions { get; set; } = new List<QuestionModel>();
}

You can bind your QuestionId and Answer values to your QuestionModel:

public class QuestionModel
{
    public int QuestionId { get; set; }
    public int Answer { get; set; }
}

By simply binding to the ViewModel in your HttpPost action:

[HttpPost]
    public ActionResult PostAction(QuestionReviewViewModel vm)
    {
        if (ModelState.IsValid)
        {
            for (int i = 0; i < vm.Questions.Count; i++)
            {
                int id = vm.Questions[i].QuestionId;
                int answer = vm.Questions[i].Answer;
            }

            return Content("Something good");
        }
        else
        {
            return Content("Something rotten");
        }
    }

Post back example

EDIT

I would use a SelectList in this case -> https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.selectlist?view=aspnet-mvc-5.2

You would add a SelectList property to your QuestionModel (see doc above for arguments), which allows you to set a selected value (Answer in this case):

// List of possible answers to this question
    // populated from data source
    public List<int> AnswerList = new List<int> { 20, 40, 60, 80, 100 };

    // An MVC SelectList Class
    public SelectList PossibleAnswers => new SelectList(AnswerList, Answer);

Then, in your view, you can loop through the possible answers, conditionally applying the checked property if the item is selected:

<div class="col-12 pt-2 pb-2">
            @foreach (var item in Model.Questions[i].PossibleAnswers)
                {
                    var htmlAttr = new Dictionary<string, object>{{ "@class", "form-check-input" }};

                if (item.Selected)
                {
                    htmlAttr.Add("@checked", true);
                }

                <div class="form-check-inline">
                    <label class="form-check-label" for="radio2">
                        @Html.RadioButtonFor(model => model.Questions[i].Answer, item.Text, htmlAttributes: htmlAttr)
                        @item.Text
                    </label>
                </div>
            }
        </div>

Upvotes: 1

Related Questions