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; }
}