0

I'm making a feedback form on mvc 5 and the feedback form consists of 10 questions having 5 selected answers each. Those selected answers are the group of radio buttons that I'm populating. But in post action method, the radio buttons are not getting bind. Below is my code.

Model.cs

public class FeedbackForm
    {
        public string Id { get; set; }
        public Models.Questionnaire mquestions { get; set; }
        public List<Questionnaire> Question { get; set; }
        public Batches batches { get; set; }
        public User user { get; set; }
        public DateTime Datetime { get; set; }


    }

    public class Questionnaire
    {
        public string QuestionId { get; set; }
        public string QuestionTitle { get; set; }
        [Required]
        public int? SelectedAnswer { get; set; } // for binding
        public IEnumerable<QuestionnaireStatus> PossibleAnswers { get; set; }
    }

    public class QuestionnaireStatus
    {
        public string StatusId { get; set; }
        public string StatusTitle { get; set; }
    }

Controller.cs

public ActionResult Index()
        {
            List<Models.FeedbackForm> lstfeedback = new List<Models.FeedbackForm>();
            Models.FeedbackForm form = new Models.FeedbackForm();


            Context.FeedBackForm contFeedback = new Context.FeedBackForm();

            Models.FeedbackForm vm = new Models.FeedbackForm();

            form.Question = new List<Models.Questionnaire>();
            form.Question = contFeedback.GetAllQuestionnaire();

            for (int i = 0; i < form.Question.Count; i++)
            {

                lstfeedback.Add(new Models.FeedbackForm
                {
                    Question = new List<Models.Questionnaire>()
                    { new Models.Questionnaire
                    {
                        QuestionId = form.Question[i].QuestionId,
                        QuestionTitle = form.Question[i].QuestionTitle
                    ,
                        PossibleAnswers = contFeedback.GetAllStatus()
                    },

                }
                });
            }


            return View(lstfeedback);
        }

View.cshtml

@using (Html.BeginForm())
{
    <table class="table">
        <tr>
            <th>
                @*@Html.Display("Questions")*@
            </th>

            @for (int i = 0; i < Model.Count; i++)
            {
                for (int j = 0; j < Model[i].Question.Count; j++)
                {

                        foreach (var item in Model[i].Question[j].PossibleAnswers)
                        {
                            <th>
                                @item.StatusTitle
                            </th>

                        }
                    break;
                }
                break;
            }

        </tr>

        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>

                @for (int j = 0; j < Model[i].Question.Count; j++)
                {
                    <td>
                        @Html.DisplayFor(m=>Model[i].Question[j].QuestionTitle)
                    </td>

                    foreach (var item in Model[i].Question[j].PossibleAnswers)
                    {
                        <td>
                            @Html.RadioButtonFor(m => Model[i].Question[j].SelectedAnswer, item.StatusId, new { id = item.StatusId })
                        </td>
                    }

                    @Html.ValidationMessageFor(m => Model[i].Question[j].SelectedAnswer)

                }

            </tr>
        }

    </table>
    <div class="form-group">
        <div class="col-md-10">
            <input type="submit" id="Attendance" value="Submit" class="btn btn-default" />
        </div>
    </div>
}
Salar Muhammad
  • 334
  • 1
  • 8
  • 21
  • You models are not really making sense. you want to bind the selected radio button to a `int? SelectedAnswer` property, but the `PossibleAnswers` is typeof `QuestionnaireStatus` which does not contain any `int` properties (and the name `QuestionnaireStatus` does not sound right anyway - is that really the class that describes a 'possible answer'?) –  Mar 07 '18 at 05:57
  • Yes it does, I actually took some idea from this link https://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons – Salar Muhammad Mar 07 '18 at 06:01
  • No it does not - it contains 2 `string` properties! And that is my answer :) –  Mar 07 '18 at 06:02
  • And you have not even shown your POST method –  Mar 07 '18 at 06:03
  • Oh yeah you are right. It was the simple mistake i was doing. Thanks anyway. Now its working. Thank you @StephenMuecke :) – Salar Muhammad Mar 07 '18 at 06:12
  • And suggest you name you classes to be a bit more descriptive of what they actually represent :) –  Mar 07 '18 at 06:13
  • Sure, I will. Thanks again :) – Salar Muhammad Mar 07 '18 at 06:15

0 Answers0