HTML表单中的空值来自MVC视图
本文关键字:MVC 视图 空值 表单 HTML | 更新日期: 2023-09-27 18:05:42
我想保存用户的步骤,并在我的MVC索引视图的每一行是一个表单。
我正在使用HTML。在foreach中开始form,以构建单个表单。页面呈现没有错误,表单提交没有问题,但我不能绑定到控制器中的模型-因此所有提交的数据都丢失了。
我已经验证了表单值是在提交表单时开始的: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 = "Index";
// 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
…
将EditFromChecklist
动作的参数从finalizeStepPassed
更改为item
。
或者你可以使用分部视图来提交你的数据。
_FinalizeStepPartial.cshtml
@model FinalizeStepViewModel
using (Html.BeginForm("EditFromChecklist", "FinalizeSteps"))
{
@Html.EditorFor(model => model.Completed)
// rest of your form
}
和在主视图中调用局部
@foreach (var item in Model)
{
@Html.Partial("_FinalizeStepPartial",item)
}
你应该绑定到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)
}
这里是一个早期的主题,也讨论了这一点:如何通过IEnumerable列表控制器在MVC包括复选框状态?