System.NullReferenceException
is thrown everytime I try to insert the data into a database.
I try to pass a model to a controller action, but obviously I send a null.
[HttpGet]
public ActionResult Answer(int step = -1, int techniqueId = -1)
{
StepsAnswersViewModel model = new StepsAnswersViewModel
{
Step = db.Steps
.Where(x => x.TechniqueId == techniqueId & x.Order == step)
.Select(x => x)
.First()
};
ViewBag.step = step;
ViewBag.tId = techniqueId;
return View(model);
}
[HttpPost]
public ActionResult Answer(StepsAnswersViewModel model)
Answer.chtml:
@model BachelorProject.Models.StepsAnswersViewModel
@{
ViewBag.Title = "Answer";
}
@Html.Partial("_Test", Model)
_Test.chtml:
@model BachelorProject.Models.StepsAnswersViewModel
@{
ViewBag.Title = "Answer";
}
<h2>Answer</h2>
@using (Html.BeginForm("Answer", "Home",
new { step = ViewBag.step + 1, echniqueId = 1 },
FormMethod.Post))
{
<label>@Model.Step.Text</label>
@Model.Step.Id
@Html.TextBoxFor(m => m.Text)
<input type="submit" value="Next Step" />
}
When I click "Next Step", null is passed to the "Answer" action for a Post requests...
StepsAnswersViewModel:
public class StepsAnswersViewModel
{
public Step Step { get; set; }
public string Text { get; set; }
}