0

I have in model having the questions and answer list. In some of the questions having 4 options and some of the questions having the 2 options. I am getting the options from the back end and i'm binding from model class into the razor view. Now if the answer value null means radio button is showing but i don't want to radio button if the radio button option value nulls.

   @for (int j = 0; j < Model.Count; j++)
                            {
                                i++;
                                <div class="panel-heading online-test" role="tab" id="">
                                    <h5 class="panel-title">
                                        @Html.HiddenFor(m => m[j].ID)
                                        @Html.HiddenFor(m => m[j].SkillId)
                                        @i) @Html.DisplayFor(m => m[j].QuestionText)
                                    </h5>
                                </div>
     foreach (var k in Model[j].Options)
                                {
                                    <div class="panel-collapse">
                                        <div class="panel-body online-test-options">
                                            <div class="input-group">
                                                <div class="col-lg-12" id="radiolist">
                                                    @Html.RadioButtonFor(m => m[j].SelectedAnswer, "A")
                                                    <label for="@k.AnswerId">@k.Option1</label>
                                                </div>
                                                <div class="col-lg-12">
                                                    @Html.RadioButtonFor(m => m[j].SelectedAnswer, "B")
                                                    <label for="@k.AnswerId">@k.Option2</label>
                                                </div>
                                                <div class="col-lg-12">
                                                    @Html.RadioButtonFor(m => m[j].SelectedAnswer, "C")
                                                    <label for="@k.AnswerId">@k.Option3</label>
                                                </div>
                                                <div class="col-lg-12">
                                                    @Html.RadioButtonFor(m => m[j].SelectedAnswer, "D")
                                                    <label for="@k.AnswerId">@k.Option4</label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                }
                         }



public class QuestionsModel
    {
        public decimal ID { set; get; }
        public decimal SkillId { get; set; }
        public string QuestionText { set; get; }
        public List<Answer> Options { set; get; }
        public string SelectedAnswer { set; get; }
        public QuestionsModel()
        {
            Options = new List<Answer>();
        }
    }
    public class Answer
    {
        public int AnswerId { get; set; }
        public string Option1 { set; get; }
        public string Option2 { set; get; }
        public string Option3 { set; get; }
        public string Option4 { set; get; }
    }
    public class Evaluation
    {
        public List<QuestionsModel> Questions { set; get; }
        public Evaluation()
        {
            Questions = new List<QuestionsModel>();
        }
    }

Please help to solve.. Thanks in Advance.

Jagan
  • 1
  • 4
  • Not clear what you asking - do you want to have an extra option for each answer which allow the user to select `null` for the `SelectedAnswer` (e.g. to mean they have not answered it, or its not applicable)? –  Jan 14 '16 at 09:50
  • And your labels - `` - make no sense as labels - you do not have assocated radio buttons with `name="k.AnswerId"` –  Jan 14 '16 at 09:51
  • And you have also stated some questions have 2 answers and some have 4 which means you model(s) are not correct anyway. You need a `Question` model describing the question with a collection property listing the possible answers for that question. –  Jan 14 '16 at 10:28
  • That wont work is you want a variable number of possible answers for each question :). Suggest you look at [this answer](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) –  Jan 14 '16 at 10:38
  • but i'm getting the correct way to get the all option list but the the thing if the answer value null means radio button is also displaying. if some of the questions having 4 options and some of the question having the 2 options( in thin case i'm getting the radio button for other 3 and 4 option if answer null). – Jagan Jan 14 '16 at 10:43
  • Its not correct. If there are only 2 possible options for a question, you still display 4 options for it! Change your model so it represents what you want to display/edit –  Jan 14 '16 at 10:45

0 Answers0