0

I'm having a hard time getting input from my view to my controller. Answers to true false questions are not getting posted with my submit button. Question object is null, database is empty except UserID. At this point I am throwing everything at the wall and nothing is sticking. Any help would be appreciated.

QuestionController:

[HttpPost]
public ActionResult Submit(FormCollection form[Bind(Include = "ID,Title,QuestionText,AnswerOptionString,AnswerOptionInt,AnswerOptionBool")] Question question) 
{
    Answer answer = new Answer();
    answer.AnswerGivenBool = question.AnswerOptionBool;
    ApplicationDbContext context = new ApplicationDbContext();
    var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    var user = UserManager.FindById(User.Identity.GetUserId());
    if (ModelState.IsValid)
    {
        string UserID = user.Id;
        answer.UserID = UserID;
        question.Answers.Add(answer);
        //db.Questions.Add(question.AnswerOptionBool);
        //db.Answers.Add(answer);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View();//Question
}

EditorTemplate: shows questions and buttons, calls submit method, but no data

@model IEnumerable<SurvApe.Models.Question>
<script src="http://code.jquery.com/jquery-latest.js"
    type="text/javascript"></script>

<script type="text/javascript">
$(function () {
 $('#btnRadio').click(function () {
      var checkedradio = $('[name="data"]:radio:checked').val();
      $("#sel").html("Selected Value: " + checkedradio);
  });
});
</script>

@using (Html.BeginForm("Submit", "Questions"))
{
<form>
    <ol class="listQuestion">
        @foreach (var item in Model)
        {
            @Html.DisplayFor(modelItem => item.Title)
            <li class="listQuestion">
               <b>True</b> @Html.RadioButton(item.QuestionText, item.Answers, new { name = "data", value = "true" })
               <b>False</b> @Html.RadioButton(item.QuestionText, item.Answers, new { name = "data", value = "false" })
               <p id="sel"></p><br />

            </li>
        }

    </ol>
    @*<input id="btnRadio" type="button" value="Get Selected Value" />*@
    <input id="btnRadio" type="submit" value="submit" />

</form>
}

Models:

public class Question
{
    public int ID { get; set; }
    public string Title { get; set; }
    public virtual Pollster pollster { get; set; }
    public string QuestionText { get; set; }
    public string AnswerOptionString { get; set; }
    public int AnswerOptionInt { get; set; }
    public bool AnswerOptionBool { get; set; }
    public QuestionType Type { get; set; }
    public string UserID { get; set; }
    public virtual List<Answer> Answers { set; get; }
    public Question()
    {
        Answers = new List<Answer>();
    }
}

public class Answer
{
    public int ID { get; set; }
    public virtual Question question { get; set; }
    public string Title { get; set; }
    public string QuestionText { get; set; }
    public string AnswerGivenString { get; set; }
    public int AnswerGivenInt { get; set; }
    public bool AnswerGivenBool { get; set; }
    public string UserID { get; set; }
}
Mcbrd
  • 1
  • 1
  • lol, looks gorgeous, sorry. Saving to sqlserverdatabase – Mcbrd Mar 01 '16 at 03:42
  • You cannot use a `foreach` loop to generate form controls for a collection. –  Mar 01 '16 at 03:46
  • Thanks for the link Mr. Muecke. I had not come across that in my days of searching. I will try to use the for loop with my editor template and bring this nightmare to an end. – Mcbrd Mar 01 '16 at 04:16
  • No, you are misunderstanding. You use either a `for` loop or you use an `EditorTemplate` (in your case its named `/Views/Shared/EditorTemplates/Question.cshtml` and its model is `@model Question` –  Mar 01 '16 at 04:18
  • But there are numerous other errors in your code. For example, the POST method needs to `public ActionResult Submit(List model)` and you use of `RadioButton()` simply makes so sense. –  Mar 01 '16 at 04:20
  • Fair enough. I had wanted to use radio buttons, but I will try with checkboxes. I'm trying both solutions separately, fixing other mistakes you pointed out, but not making much progress. – Mcbrd Mar 01 '16 at 21:59
  • Whether its radio button or checkboxes makes no difference. Its you use of the `RadioButton()` method that makes no sense. I suggest you start by looking at [this answer](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) to get you started. Sorry to be harsh, but there is almost nothing in your code, including your models, that makes sense. –  Mar 01 '16 at 22:25

0 Answers0