Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

C# MVC - Help with simple Dynamic Form

Hi,

I am struggling to resolve this, I am creating a basic questionnaire, with questions and radio button answers on my View.

Its dynamic in the sense that, the questions and radio buttons are database driven, so amending that affects the number of questions on the page.

The trouble is, when I submit the form, the method I hit (my HTTPPOST action] doesn't receive the data from the form.

Here is my view:

@model IEnumerable<HandHygiene.ViewModel.QuestionViewModel>

@{
    ViewData["Title"] = "Home Page";
}

<h4>Questions</h4>

<form asp-action="Create" method="post">
    @foreach (var item in Model)
    {
        <div>
            <p>
                @Html.DisplayFor(modelItem => item.QuestionName)
            </p>
            <p>
                @foreach (var obj in item.QuestionOptions)
                {
                    <input type="radio" name="@item.QuestionID" value="@obj" /> @Html.Label(obj)
                }    

            </p>
        </div>

    }


    <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-default" />
    </div>

</form>

Here is my HttpPost action method that is hit upon submit:

        [HttpPost]
        public IActionResult Create(IEnumerable<AnswerViewModel> answerViewModel)
        {



            return null;
        }

Here is the AnswerViewModel

    public class AnswerViewModel
    {

        public int SelectedAnswer { get; set; }
        public int QuestionID { get; set; }
    }

Any ideas would be appreciated,

thanks