title: MVC 5 ASP.NET Entity Framework Collect Form Data through Range Input
body: <p>So I am creating a survey monkey, a Survey Has 4 categories, and each category has 5 questions, the HomeController Index passes on the 20 questions from the Questions entity and each Questions must have a Range Slider with values from one to 5, the form values must be collected into a method in the controller, then accumulated into the QuestionResults table and processed, then they must be passed onto the CategoryResults and each Category, out of the 4, must have an accumulated score, they must then be compared to the CategoryFeedback(which is static) table and display the correct information. The code for my HomeController and it's index is as below.</p>
  
  <p>The main issue that I am having, is that since I am passing on the form values through as a parameter of FormCollection, I am struggling to keep the relationship of a Question belonging to a Category and then passing these all onto the CategoryResult table for processing.</p>
  
  <p><strong>Any help will be greatly appreciated</strong></p>
  
  <p><strong>Models:</strong></p>
  
  <p><strong>Question</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace STRA.Models
  {
      public class Question
      {
          public int Id { get; set; }
          public string Title { get; set; }
          public string CreatedBy { get; set; }
          public DateTime? DateCreated { get; set; }
          public DateTime? DateModified { get; set; }
          public virtual Category Category { get; set; }
          public int CategoryId { get; set; }
          public virtual ICollection&lt;QuestionResult&gt; QuestionResult { get; set; }
          public virtual ICollection&lt;QuestionFeedback&gt; QuestionFeedback { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>QuestionResult</strong></p>
  
  <pre><code>using IdentitySample.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace STRA.Models
  {
      public class QuestionResult
      {
          public int Id { get; set; }
          public DateTime? DateCreated { get; set; }
          public DateTime? DateModified { get; set; }
          public int QuestionScore { get; set; }
          //navigation properties
          public virtual ApplicationUser User { get; set; }
          public ICollection&lt;CategoryResult&gt; CategoryResult { get; set; }
          public virtual Question Question { get; set; }
          public int QuestionId { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>CategoryResult</strong></p>
  
  <pre><code>using IdentitySample.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace STRA.Models
  {
      public class CategoryResult
      {
          public int Id { get; set; }
          public int CategoryScore {get;set;}
          //navigation properties
          public virtual QuestionResult QuestionResult { get; set; }
          public int QuestionResultId { get; set; }
  
      }
  }
  </code></pre>
  
  <p><strong>Any help will be greatly appreciated.</strong>
  <strong>HomeController Index</strong></p>
  
  <pre><code>@model IEnumerable&lt;STRA.Models.Question&gt;
  @{
      //ViewBag.Question.Title = "Survey";
      Layout = "~/Views/Shared/_QuizLayout.cshtml";
  
  }
  
  &lt;div class="hr hr-18 hr-double dotted"&gt;&lt;/div&gt;
  
  &lt;div class="widget-box"&gt;
      &lt;div class="widget-header widget-header-blue widget-header-flat"&gt;
          &lt;h4 class="widget-title lighter"&gt;STRA&lt;/h4&gt;
  
          @*&lt;div class="widget-toolbar"&gt;
                  &lt;label&gt;
                      &lt;small class="green"&gt;
                          &lt;b&gt;Validation&lt;/b&gt;
                      &lt;/small&gt;
  
                      &lt;input id="skip-validation" type="checkbox" class="ace ace-switch ace-switch-4" /&gt;
                      &lt;span class="lbl middle"&gt;&lt;/span&gt;
                  &lt;/label&gt;
              &lt;/div&gt;*@
      &lt;/div&gt;
      @*"Create","Home"*@
      @using (Html.BeginForm("Create", "Home", FormMethod.Post))
      {
          @Html.AntiForgeryToken()
  
          &lt;div class="widget-body"&gt;
              &lt;div class="widget-main"&gt;
                  &lt;!-- #section:plugins/fuelux.wizard --&gt;
                  &lt;div id="fuelux-wizard-container"&gt;
                      &lt;div class="col-md-12 center"&gt;
                          &lt;div class="easy-pie-chart percentage" data-percent="25" data-color="#2679b5"&gt;
                              @*&lt;span class="percent"&gt;20&lt;/span&gt;%*@
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div style="display:none;"&gt;
  
                          &lt;!-- #section:plugins/fuelux.wizard.steps --&gt;
                          &lt;ul class="steps"&gt;
                              &lt;li data-step="1" class="active"&gt;
                                  &lt;span class="step"&gt;1&lt;/span&gt;
                                  &lt;span class="Question.Title"&gt;Validation states&lt;/span&gt;
                              &lt;/li&gt;
  
                              &lt;li data-step="2"&gt;
                                  &lt;span class="step"&gt;2&lt;/span&gt;
                                  &lt;span class="Question.Title"&gt;Alerts&lt;/span&gt;
                              &lt;/li&gt;
  
                              &lt;li data-step="3"&gt;
                                  &lt;span class="step"&gt;3&lt;/span&gt;
                                  &lt;span class="Question.Title"&gt;Payment Info&lt;/span&gt;
                              &lt;/li&gt;
  
                              &lt;li data-step="4"&gt;
                                  &lt;span class="step"&gt;4&lt;/span&gt;
                                  &lt;span class="Question.Title"&gt;Other Info&lt;/span&gt;
                              &lt;/li&gt;
                          &lt;/ul&gt;
  
                          &lt;!-- /section:plugins/fuelux.wizard.steps --&gt;
                      &lt;/div&gt;
  
                      &lt;hr /&gt;
  
                      &lt;!-- #section:plugins/fuelux.wizard.container --&gt;
                      &lt;div class="step-content pos-rel"&gt;
  
  
                          &lt;div class="step-pane" data-step="1"&gt;
                              @{
  
          foreach (var item in Model.Take(5))
          {
  
              &lt;div class="col-md-12"&gt;
                  &lt;h4&gt;@Html.DisplayFor(modelItem =&gt; item.Title)&lt;/h4&gt;
  
                  &lt;div id="slider-eq"&gt;
                      &lt;h5 class="pull-left"&gt;Strongly Disagree&lt;/h5&gt;
                      &lt;h5 class="pull-right"&gt;Strongly Agree&lt;/h5&gt;
                      &lt;span id="q_@Html.DisplayFor(modelItem =&gt; item.Id)" class="ui-slider-purple"&gt;3&lt;/span&gt;
                      &lt;input type="hidden" id="question_@item.Id" name="question_@item.Id" value="3" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
          }
          //Model.Skip(5);
                              }
                          &lt;/div&gt;
                          &lt;div class="step-pane" data-step="2"&gt;
                              @*&lt;div class="center"&gt;
                                      &lt;h3 class="blue lighter"&gt;This is step 2&lt;/h3&gt;
                                  &lt;/div&gt;*@
                              @{
          foreach (var item in Model.Skip(5).Take(5))
          {
  
  
              &lt;div class="col-md-12"&gt;
                  &lt;h4&gt;@Html.DisplayFor(modelItem =&gt; item.Title)&lt;/h4&gt;
                  &lt;div id="slider-eq"&gt;
                      &lt;h5 class="pull-left"&gt;Strongly Disagree&lt;/h5&gt;
                      &lt;h5 class="pull-right"&gt;Strongly Agree&lt;/h5&gt;
                      &lt;span id="q_@Html.DisplayFor(modelItem =&gt; item.Id)" class="ui-slider-purple"&gt;3&lt;/span&gt;
                      &lt;input type="hidden" id="question_value" name="question_value" value="3" /&gt;
                      &lt;input type="hidden" id="questonId" name="questonId" value="@item.Id" /&gt;
                             @*&lt;span class="ui-slider-red"&gt;55&lt;/span&gt;
                      *@&gt;&lt;/div&gt;
              &lt;/div&gt;
  
          }
                              }
                          &lt;/div&gt;
  
                          &lt;div class="step-pane" data-step="3"&gt;
                              @*&lt;div class="center"&gt;
                                      &lt;h3 class="blue lighter"&gt;This is step 3&lt;/h3&gt;
                                  &lt;/div&gt;*@
                              @{
          foreach (var item in Model.Skip(10).Take(5))
          {
  
  
              &lt;div class="col-md-12"&gt;
                  &lt;h4&gt;@Html.DisplayFor(modelItem =&gt; item.Title)&lt;/h4&gt;
                  &lt;div id="slider-eq"&gt;
                      &lt;h5 class="pull-left"&gt;Strongly Disagree&lt;/h5&gt;
                      &lt;h5 class="pull-right"&gt;Strongly Agree&lt;/h5&gt;
                      &lt;span id="q_@Html.DisplayFor(modelItem =&gt; item.Id)" class="ui-slider-purple"&gt;3&lt;/span&gt;
                      &lt;input type="hidden" id="question_@item.Id" name="question_@item.Id" value="3" /&gt;
                      @*&lt;span class="ui-slider-red"&gt;55&lt;/span&gt;
                      *@
                  &lt;/div&gt;
              &lt;/div&gt;
  
          }
                              }
                          &lt;/div&gt;
  
                          &lt;div class="step-pane" data-step="4"&gt;
                              @*&lt;div class="center"&gt;
                                      &lt;h3 class="blue lighter"&gt;This is step 4&lt;/h3&gt;
                                  &lt;/div&gt;*@
                              @{
          foreach (var item in Model.Skip(15).Take(5))
          {
  
  
              &lt;div class="col-md-12"&gt;
                  &lt;h4&gt;@Html.DisplayFor(modelItem =&gt; item.Title)&lt;/h4&gt;
                  &lt;div id="slider-eq"&gt;
                      &lt;h5 class="pull-left"&gt;Strongly Disagree&lt;/h5&gt;
                      &lt;h5 class="pull-right"&gt;Strongly Agree&lt;/h5&gt;
                      &lt;span id="q_@Html.DisplayFor(modelItem =&gt; item.Id)" class="ui-slider-purple"&gt;3&lt;/span&gt;
                      &lt;input type="hidden" id="question_@item.Id" name="question_@item.Id" value="3" /&gt;
                      @*&lt;span class="ui-slider-red"&gt;55&lt;/span&gt;
                      *@
                  &lt;/div&gt;
              &lt;/div&gt;
  
          }
                              }
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;!-- /section:plugins/fuelux.wizard.container --&gt;
                  &lt;/div&gt;
  
                  &lt;hr /&gt;
                  &lt;div class="wizard-actions"&gt;
                      &lt;!-- #section:plugins/fuelux.wizard.buttons --&gt;
                      &lt;button type="button" id="previous" class="btn btn-prev"&gt;
                          &lt;i class="ace-icon fa fa-arrow-left"&gt;&lt;/i&gt;
                          Prev
                      &lt;/button&gt;
  
                      &lt;button type="button" id="next" class="btn btn-success btn-next" @*data-last="Finish"*@&gt;
                          Next
                          &lt;i class="ace-icon fa fa-arrow-right icon-on-right"&gt;&lt;/i&gt;
                      &lt;/button&gt;
                      &lt;button id="finish" class="btn btn-success" type="submit"&gt;
                          Submit
                          &lt;i class="ace-icon fa fa-arrow-right icon-on-right"&gt;&lt;/i&gt;
                      &lt;/button&gt;
                      &lt;!-- /section:plugins/fuelux.wizard.buttons --&gt;
                  &lt;/div&gt;
  
                  &lt;!-- /section:plugins/fuelux.wizard --&gt;
              &lt;/div&gt;&lt;!-- /.widget-main --&gt;
          &lt;/div&gt;
      &lt;!-- /.widget-body --&gt;
      }
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>HomeController</strong></p>
  
  <pre><code>using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  using IdentitySample.Models;
  using STRA.Models;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  
  namespace IdentitySample.Controllers
  {
      [Authorize]
      public class HomeController : Controller
      {
          private ApplicationDbContext db = new ApplicationDbContext();
          //public ActionResult Index()
          public ActionResult Index()
          {
              if (User.IsInRole("Admin"))
              {
                  return RedirectToAction("Index", "Dashboard");
              }
              if (User.IsInRole("Sales Consultant"))
              {
                  return RedirectToAction("Index", "Reports");
              }
  
  
              //loads all questions in
              //var questions = db.Questions.Include(s =&gt; s.Survey).Include(c =&gt; c.Category);
  
              var questions = db.Questions.Include(c =&gt; c.Category);
              return View(questions.ToList());
              //var questionResults = db.QuestionResults.Include(c =&gt; c.Question);
              //return View(questionResults.ToList());
  
              //viewmodel attempt
              //var viewModel = new SurveyViewModels();
              //viewModel.Questions = db.Questions
              //    .Include(i =&gt;)
          }
  
          [HttpPost]
          public ActionResult Index(FormCollection result)
          {
              List&lt;QuestionResult&gt; info = new List&lt;QuestionResult&gt;();
              QuestionResult newResult = new QuestionResult();
              var appUser = new ApplicationUserManager(new UserStore&lt;ApplicationUser&gt;(db));
              ApplicationUser user = appUser.FindById(User.Identity.GetUserId());
              foreach (var key in result.AllKeys.Where(k =&gt; k.Contains("question")).ToArray&lt;string&gt;())
              {
                  string[] keys = key.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
  
                  if (key.Count() &gt; 1)
                  {
                      QuestionResult re = new QuestionResult();
                      re.QuestionScore = Convert.ToInt32(result[key]);
                      re.QuestionId = Convert.ToInt32(keys[1]);
                      re.User = user;
                      db.QuestionResults.Add(re);
                  }
              }
  
              db.SaveChanges();
  
              Debug.WriteLine(result[0]);
              return View();
          }
  
      }
  }
  </code></pre>
  
  <p><strong>jQuery UI Slider Plugin</strong></p>
  
  <pre><code>$("#slider-eq &gt; span").css({ width: '100%', 'float': 'left', margin: '15px' }).each(function () {
              // read initial values from markup and remove that
              var value = parseInt($(this).text(), 10);
              $(this).empty().slider({
                  value: value,
                  min: 1,
                  max: 5,
                  range: "min",
                  animate: true,
                  change: function (event, ui) {
                      //alert(ui.value);
                      $(this).next("[id^=question_]").val(ui.value);
                      //$(this &gt; ).slider("values", $this.data("index"), $this.val());
                  }
  
              });
          });
  </code></pre>