将调查问题的答案保存到sql server mvc 5

时间:2016-03-01 03:42:04

标签: c# asp.net-mvc-5

我很难从我的视图输入到控制器。使用我的提交按钮不会发布真假问题的答案。 Question对象为null,除UserID外,数据库为空。在这一点上,我把一切扔在墙上,什么都没有。任何帮助将不胜感激。

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:显示问题和按钮,调用提交方法,但没有数据

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

型号:

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

0 个答案:

没有答案