表单POST正在向控制器发送空值。 [英] The form POST is sending null value to controller.
本文介绍了表单POST正在向控制器发送空值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过表单更新数据库中的多行。 Th值在foreach循环中。我正在尝试更新表单中回答字段的几行。请找到以下代码:
I am trying to update several rows in my database through a form. Th values are in foreach loop. I am trying to update several rows for the field 'Answer' in the form. Please find the code below:
View:
@model IEnumerable<VOC.Models.QuestionnaireFeedback>
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("updateFeedback", "Questionnaire", FormMethod.Post))
{
@foreach(var item in Model){
@Html.DisplayFor(modelItem => item.ProjectID)
@Html.DisplayFor(modelItem => item.FeedbackNo)
@Html.EditorFor(modelItem => item.Answer)
}
<input type="submit" value="Save" />
}
Controller:
<pre>[HttpPost]
public ActionResult updateFeedback(List<QuestionnaireFeedback> fromQuestionnaire)
{
if (ModelState.IsValid) {
FeedbackBusinessLayer feedbcakBusLayr = new FeedbackBusinessLayer();
feedbcakBusLayr.updateFeedback(fromQuestionnaire);
return RedirectToAction("Index");
}
return View(db.QuestionnaireFeedbacks.ToList());
}
我对MVC的概念比较陌生。但是,我调试了代码,在Controller fromQuestionnaire 中为null。
我已经被困了一段时间了。任何帮助将不胜感激。
I am relatively new to the concept of MVC. But,I debugged the code, in the Controller fromQuestionnaire is null.
I have been stuck for a while now. Any help will be appreciated.
推荐答案
您需要使用基于数组的方法而不是foreach。
http://haacked.com/archive/ 2008/10/23 / model-binding-to-a-list.aspx / [ ^ ]
Rather than foreach in your view you need to use an array-based approach.
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/[^]
这篇关于表单POST正在向控制器发送空值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文