[英]MVC Model is null on post
My viewmodel works fine on GET, but on post it is null and I cannot understand why. 我的viewmodel在GET上运行正常,但在帖子上它是null,我无法理解为什么。 Any help would be greatly appreciated.
任何帮助将不胜感激。 The Quiz-post doesn't do much at this stage, I need to access the posted model before I can finish the method.
测验帖在这个阶段做得不多,我需要在完成方法之前访问已发布的模型。 The repository.GetAllQuestionsList() returns a list of QuizViewModel
repository.GetAllQuestionsList()返回QuizViewModel列表
ViewModel 视图模型
public class QuizViewModel {
public int Id { get; set; }
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public int QuestionNbr { get; set; }
public int CurrentQuestion { get; set; }
public int TotalNbrOfQuestions { get; set; }
public List<Answer> Answers { get; set; }
}
Controller 调节器
public ActionResult Quiz() {
var getAllQuestions = repository.GetAllQuestionsList();
var model = getAllQuestions.Where(c => c.QuestionNbr == 1);
Session["CurrentQ"] = 1;
return View(model);
}
[HttpPost]
public ActionResult Quiz(IEnumerable<Models.QuizViewModel> model) {
int question = Convert.ToInt32(Session["CurrentQ"]);
string str = Request.Params["btnSubmit"];
if (str == "Next Question") {
question++;
Session["CurrentQ"] = question;
}
if (str == "Prev Question") {
question--;
Session["CurrentQ"] = question;
}
return View(model);
}
View 视图
@model IEnumerable<Quiz.Models.QuizViewModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Quiz</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm()) {
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.QuestionText)
</th>
<th>
@Html.DisplayNameFor(model => model.QuestionNbr)
</th>
<th>
answer
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.QuestionText)
</td>
<td>
@Html.DisplayFor(modelItem => item.QuestionNbr)
</td>
<td>
@foreach (var ML in item.Answers) {
@Html.RadioButtonFor(model => item.Answers, ML.Id)
@Html.Label(ML.AnswerText)
<br/>
}
</td>
</tr>
}
</table>
<input type="submit" id="Submit1" name="btnSubmit" value="Prev Question"/>
<input type="submit" id="Submit2" name="btnSubmit" value="Next Question"/>
}
</body>
</html>
I dont't think your code will work ever as the name binding are not generated properly also @Html.DisplayNameFor(model => model.QuestionText)
and @Html.DisplayNameFor(model => model.QuestionNbr)
will throw exception as Model is of type IEnumerable. 我不认为你的代码会工作,因为名称绑定也没有正确生成
@Html.DisplayNameFor(model => model.QuestionText)
和@Html.DisplayNameFor(model => model.QuestionNbr)
将抛出异常,因为Model是IEnumerable类型。 HttpPost binding works only if the name are input controls are generated appropriately in generated html when posted to server. 只有当名称是输入控件时,HttpPost绑定才能在生成的html中生成,当发布到服务器时。 I have corrected your code.Please refer the below
我已经更正了你的代码。请参阅下面的内容
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Quiz</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm()) {
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model[0].QuestionText)
</th>
<th>
@Html.DisplayNameFor(model => model[0].QuestionNbr)
</th>
<th>
answer
</th>
<th></th>
</tr>
@for (int index = 0; index > Model.Count; index++) {
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[index].QuestionText)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[index].QuestionNbr)
</td>
<td>
@for (int jIndex = 0; jIndex < Model[index].Answers; jIndex++ ) {
@Html.RadioButtonFor(model => modelItem[index].Answers[jIndex].Answer, modelItem[index].Answers[jIndex].Id)
@Html.Label(modelItem[index].Answers[jIndex].AnswerText)
<br/>
}
</td>
</tr>
}
</table>
<input type="submit" id="Submit1" name="btnSubmit" value="Prev Question"/>
<input type="submit" id="Submit2" name="btnSubmit" value="Next Question"/>
}
</body>
</html>
Ignoring the @Html.DisplayNameFor(model => model.QuestionText)
which (as I said doesn't make sense). 忽略
@Html.DisplayNameFor(model => model.QuestionText)
(正如我所说的那样没有意义)。 You just need to change you iterator so that it knows where in the array the item lives, ie 您只需要更改迭代器,以便它知道项目在数组中的位置,即
@for (int i = 0; i < Model.Count; i++) {
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[i].QuestionText)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].QuestionNbr)
</td>
..... etc
}
foreach
doesn't work very well in an MVC view because the HTML helper doesn't know where in the array it is and the names aren't built up correctly. foreach
在MVC视图中不能很好地工作,因为HTML帮助程序不知道它在数组中的位置,并且名称没有正确构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.