[英]Strongly-typed view not initializing?
我正在一个项目请求网站上工作,充实了一篇文章,在该文章中,从事项目工作的员工可以估算完成一个项目区域所需的时间。 我希望此页面将任意数量的估算值发布回“编辑”操作,但是我在这里丢失了一些内容。
在我的视图中,我正在使用此处帮助器中的Html.BeginCollectionItem
来帮助处理Html.RenderPartial("_WorkEstimateEditorRow", item);
的AJAX-y内容Html.RenderPartial("_WorkEstimateEditorRow", item);
。
编辑:我现在正在为此的AJAX部分苦苦挣扎-我看到这些值又回到Request.Form
,并且这些属性正确地匹配了我的WorkEstimate
类,但是即使我将控制器操作更改为仅接受一个IEnumerable<WorkEstimate>
估计,它为null。
这是使用Html.BeginCollectionItem
一行的输出值:
<input data-val="true" data-val-number="The field EstimateId must be a number." data-val-required="The EstimateId field is required." id="estimates_d32afd89-987e-4d09-a847-abfc33dde220__EstimateId" name="estimates[d32afd89-987e-4d09-a847-abfc33dde220].EstimateId" value="0" type="hidden">
<input id="estimates_d32afd89-987e-4d09-a847-abfc33dde220__Estimator" name="estimates[d32afd89-987e-4d09-a847-abfc33dde220].Estimator" value="" type="hidden">
<span class="editor-label">
<label for="estimates_d32afd89-987e-4d09-a847-abfc33dde220__WorkArea">Work Area</label>
</span>
<span class="editor-field">
<input class="text-box single-line" id="estimates_d32afd89-987e-4d09-a847-abfc33dde220__WorkArea" name="estimates[d32afd89-987e-4d09-a847-abfc33dde220].WorkArea" value="" type="text">
<span class="field-validation-valid" data-valmsg-for="estimates[d32afd89-987e-4d09-a847-abfc33dde220].WorkArea" data-valmsg-replace="true"></span>
</span>
<span class="editor-label">
<label for="estimates_d32afd89-987e-4d09-a847-abfc33dde220__Hours">Hours</label>
</span>
<span class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Hours must be a number." data-val-required="The Hours field is required." id="estimates_d32afd89-987e-4d09-a847-abfc33dde220__Hours" name="estimates[d32afd89-987e-4d09-a847-abfc33dde220].Hours" value="0" type="number">
<span class="field-validation-valid" data-valmsg-for="estimates[d32afd89-987e-4d09-a847-abfc33dde220].Hours" data-valmsg-replace="true"></span>
</span>
<a href="#" class="deleteRow">[x]</a>
<hr>
name
属性是post操作上Request.Form
遇到的。 我也尝试过更改Controller动作,以便它接收到IEnumerable<WorkEstimate>
而不进行任何更改。
模型
public class EstimationManager
{
public EstimationManager()
{
CurrentUser = new WebUser();
Project = null;
EstimationData = new WorkEstimateRepository();
Estimates = new List<WorkEstimate>();
}
public EstimationManager(ApprovedProject project, WebUser currentUser)
: this(project, currentUser, new WorkEstimateRepository())
{ }
public EstimationManager(ApprovedProject project, WebUser currentUser, IWorkEstimateRepository repository)
{
Project = project;
CurrentUser = currentUser;
EstimationData = repository;
Estimates = EstimationData.Get(Project);
}
IWorkEstimateRepository EstimationData { get; set; }
public WebUser CurrentUser { get; set; }
public ApprovedProject Project { get; set; }
public List<WorkEstimate> Estimates { get; set; }
public bool CurrentUserHasWorkerAccess
{
get
{
return CurrentUser != null
&& CurrentUser.AccessLevels.HasWorkerAccess
&& (Project == null || CurrentUser.AccessLevels.WorkerUnit == Project.CurrentWorkerUnit);
}
}
}
控制器动作
public class EstimatesController : BaseSessionController
{
private IProjectRepository _projects;
private IWorkEstimateRepository _estimates;
EstimationManager manager;
public EstimatesController()
: this(new WorkEstimateRepository(), new ProjectRepository())
{ }
public EstimatesController(IWorkEstimateRepository estimates, IProjectRepository projects)
{
_estimates = estimates;
_projects = projects;
}
//
// GET: /Estimates/Edit/5
public ActionResult Edit(int id)
{
ApprovedProject details = _projects.Get(id);
manager = new EstimationManager(details, CurrentUser, _estimates);
return View(manager);
}
//
// POST: /Estimates/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
ApprovedProject details = _projects.Get(id);
manager = new EstimationManager(details, CurrentUser, _estimates);
if (TryUpdateModel(manager)
&& _estimates.TrySave(manager))
{
return RedirectToAction("Details", new { id = id });
}
else
{
foreach (WorkEstimate item in manager.Estimates)
{
foreach (RuleViolation currentViolation in item.GetRuleViolations())
{
ModelState.AddModelError(item.WorkArea + currentViolation.PropertyName, currentViolation.ErrorMessage);
}
}
return View(manager);
}
}
}
视图
@model ProjectRequests.Web.Models.Project.Estimates.EstimationManager
@{
ViewBag.Title = "Edit Estimate Details for " + Model.Project.Name;
}
<h2>Edit Estimate Details for @Model.Project.Name</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Estimation Manager</legend>
<span id="editorRows">
@foreach (ProjectRequests.Web.Models.Project.Estimates.WorkEstimate item in Model.Estimates)
{
if (Model.CurrentUser == item.Estimator)
{
Html.RenderPartial("_WorkEstimateEditorRow", item);
}
else
{
Html.DisplayFor(modelItem => item);
}
}
</span>
@if (Model.CurrentUserHasWorkerAccess)
{
@Html.ActionLink("Add another estimate.", "BlankEstimateRow", null, new { id = "addItem" })
<p>
<input type="submit" value="Save" />
</p>
}
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$().ready(function () {
$("#addItem").click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$("#editorRows").append(html);
}
});
return false;
});
$("#editorRows").on("click", ".deleteRow", function () {
$(this).closest(".editorRow").remove();
return false;
});
});
</script>
}
当我的Edit操作的签名是public ActionResult Edit(int id, FormCollection collection)
,该视图返回适当的数据,但是TryUpdateModel
实际上并未更新Estimates
属性。 相反, public ActionResult Edit(int id, EstimationManager newManager)
也未设置Estimates
。 手动筛选FormCollection
以从具有较大代码味道的页面区域中抽出值的想法,这使我想到应该以不同的方式处理此问题。
我有一个类似的问题。 为了使MVC可以很好地与集合一起使用,我发现最好在视图中使用for
循环而不是foreach
循环。 当您使用Html
帮助程序时,使用表达式中的索引器访问值,这将导致Razor在表单控件的名称中使用该索引器。 在您的控制器上,接受一个集合(可以肯定地使用数组,您也许可以摆脱IEnumerable<>
),它应该可以工作。
由于您使用的是类而不是直接使用集合作为模型,因此您可能需要在action参数上使用BindAttribute
来使MVC知道请求数据将具有前缀,或者使用另一个具有属性的类,与您的EstimationManager
类中的属性名称相同的操作参数。
一旦开始通过索引器访问,只需查看MVC输出的源代码,我认为表单名称将帮助您了解发生了什么。
看起来您正在遍历模型的Estimates
集合,并从每个Estimate
获取一个WorkEstimate
属性的值,然后使用该WorkEstimate
渲染部分视图或对其进行标记。
我猜因为在视图上没有带有索引的特定Estimate
项目,因此在您使用public ActionResult Edit(int id, EstimationManager newManager)
的示例中,模型绑定程序无法确定它们是否需要绑定到Estimates
集合中。 public ActionResult Edit(int id, EstimationManager newManager)
。
另外,显然具有索引值对于收集项很重要; 它比在视图中使用for
而不是foreach
和索引器更好地使用收集项,如下所示:
@for (int i = 0, i < Model.Estimates.Count; i++)
{
Html.EditorFor(m => m.Estimates[i].WorkEstimate);
}
作为一个粗略的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.