我想保存用户步骤,并将我的MVC索引视图中的每一行都作为一个表单。
我在foreach中使用HTML.Begin表单来构建各个表单。页面呈现没有错误,表单提交没有问题,但我无法绑定到控制器内的模型 - 因此所有提交的数据都会丢失。
我已经验证表单值是在提交表单时开始的:item.Completed = true& item.Completed = false& item.Comment = HELLO + WORLD +& item.FinalizeStepId = 1& item .StepId = 1,但控制器不接受它们,并且使用空值创建FinalizeStepViewModel对象。
那么如何让表单正确传回数据呢?
这可能是我在Stackoverflow上的第二个问题,所以让我知道我可能需要添加的其他信息。
感谢。
===模特=====
public class FinalizeStepViewModel
{
public int FinalizeStepId { get; set; }
// foreign key from Step table
public int StepId { get; set; }
// name of taks from Step table
public string StepDesc { get; set; }
[DisplayName("Review Data")]
public string ReviewFormulaValue { get; set; }
[Required]
public bool Completed { get; set; }
[DisplayName("Fiscal Year")]
public int FiscalYear { get; set; }
// Period for the adjustment
[Required]
public int Period { get; set; }
[Required]
public string UserID { get; set; }
[Required]
[DisplayName("Created By")]
public string CreatedBy { get; set; }
[Required]
[DisplayName("Created At")]
public DateTime CreatedAt { get; set; }
public string Comment { get; set; }
====查看========== @model IEnumerable
@ { ViewBag.Title ="索引&#34 ;;
// is everything completed, if yes => enabled
string alldone = "enabled";
}
<h2>Finalize Checklist</h2>
<table class="table">
<tr>
<th>
Completed
</th>
<th>
Finalized Task
</th>
<th>
Review Data
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
<th></th>
<th></th>
@*<th>
@Html.DisplayNameFor(model => model.FiscalYear)
</th>
<th>
@Html.DisplayNameFor(model => model.Period)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedAt)
</th>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>*@
<th></th>
</tr>
@foreach (var item in Model)
{
//<form action="/FinalizeSteps/Checklist/" method="post">
//@using (Html.BeginForm("Login", "Account", FormMethod.Post))
//// <form action="/Account/Login" action="post">
using (Html.BeginForm("EditFromChecklist", "FinalizeSteps", FormMethod.Post, new { finalizeStepPassed = Model }))
{
<tr>
<td>
<div class="form-group" style="text-align: center; vertical-align: text-top;">
<div class="checkbox">
@Html.EditorFor(modelItem => item.Completed)
@if (item.Completed == false) { alldone = "disabled"; }
</div>
</div>
</td>
<td>
<h4>@Html.DisplayFor(modelItem => item.StepDesc)</h4>
</td>
<td style="text-align: center;">
@Html.DisplayFor(modelItem => item.ReviewFormulaValue)
</td>
<td>
<div class="form-group" style="width: 300px;">
@Html.EditorFor(modelItem => item.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(modelItem => item.Comment, "", new { @class = "text-danger" })
</div>
</td>
<td>
<div class="form-group">
@Html.EditorFor(modelItem => item.FinalizeStepId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(modelItem => item.FinalizeStepId, "", new { @class = "text-danger" })
</div>
</td>
<td>
<div class="form-group">
@Html.EditorFor(modelItem => item.StepId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(modelItem => item.FinalizeStepId, "", new { @class = "text-danger" })
</div>
</td>
@*<td>
@Html.DisplayFor(modelItem => item.FiscalYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.Period)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedAt)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>*@
<td>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-default" />
</div>
@Html.ActionLink("Save", "EditFromChecklist", new { FinalizeStepId = item.FinalizeStepId, StepId = item.StepId, Completed = item.Completed, Comment = item.Comment })
@*@Html.ActionLink("Edit", "Edit", new { id = item.FinalizeStepId }) |
@Html.ActionLink("Details", "Details", new { id = item.FinalizeStepId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.FinalizeStepId })*@
</td>
</tr>
}
}
</table>
===控制器方法====
[HttpPost]
public ActionResult EditFromChecklist([Bind(Include = "FinalizeStepId,StepId,Completed,Comment")] FinalizeStepViewModel finalizeStepPassed)
{
// Do we have a FinalizeStepId?
if (finalizeStepPassed.FinalizeStepId != 0)
{
// Yes, this is an edit
...
答案 0 :(得分:0)
您应该绑定到IList而不是IEnumerable,而不是
@foreach (var item in Model)
{
@Html.EditorFor(modelItem => item.Completed)
}
使用此语法
@for( int i=0; i < Model.Count; i++ )
{
@Html.EditorFor(modelItem => Model[i].Completed)
}
以前的主题也讨论了这个问题:How to pass IEnumerable list to controller in MVC including checkbox state?
答案 1 :(得分:0)
将EditFromChecklist
操作的参数从finalizeStepPassed
更改为item
。
或者您可以使用部分视图提交数据。
_FinalizeStepPartial.cshtml
@model FinalizeStepViewModel
using (Html.BeginForm("EditFromChecklist", "FinalizeSteps"))
{
@Html.EditorFor(model => model.Completed)
// rest of your form
}
并在循环内部的主视图中调用partial
@foreach (var item in Model)
{
@Html.Partial("_FinalizeStepPartial",item)
}