System.NullReferenceException
每次嘗試將數據插入數據庫時都會引發。 我嘗試將模型傳遞給控制器操作,但顯然我發送了空值。如何將模型傳遞到視圖
[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" />
}
當我點擊 「下一步」,null被傳遞給 「答案」 行動的POST請求...
StepsAnswersViewModel:
public class StepsAnswersViewModel
{
public Step Step { get; set; }
public string Text { get; set; }
}
你能顯示'StepsAnswersViewModel'的定義嗎? – Ofiris
當然,剛剛添加 –
'步'有'ID'和'文字'字段? – Ofiris