如何更新复杂模型?我有以下观点:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tidrapportering.Models.Week>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<table width="100%">
<% foreach (var task in Model.Tasks)
{ %>
<tr>
<td>
<%: task.Customer.CustomerName %>
</td>
<td>
<%: task.TaskName %>
</td>
<td>
<% foreach (var ts in task.TimeSegments)
{ %>
<%= Html.TextBox("Hours", ts.Hours)%>
<% } %>
</td>
</tr>
<% } %>
</table>
<p>
<input type="submit" value="Spara tid" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
我尝试像往常一样更新它,只需在模型对象上调用UpdataModel:UpdateModel(week);但那没用。所以我读到了一些关于必须在复杂模型中单独更新每个属性的内容,并试图使其适应我的情况。这是我在Controller中的尝试:
[HttpPost]
public ActionResult EditTasks(int id, FormCollection collection)
{
//try
//{
Week week = _model.GetWeek(id);
foreach (var task in week.Tasks)
{
foreach (var timeSegment in task.TimeSegments)
{
UpdateModel(timeSegment.Hours, "Hours");
}
}
//UpdateModel(week);
_model.Save();
return RedirectToAction("Index");
//}
//catch
//{
// return View();
//}
}
但这也不起作用。如果属性是一个字符串似乎有效,但这是一个int,并且编译器抱怨它必须是一个用作TModel的引用类型。
我不知道这是否可行,我只需要了解如何更新这样的复杂类型模型。这不是太常见,所以必须有一些标准方法,但我无法弄清楚......
有什么想法吗?
更新
以下作品:
行动方法:
[HttpPost]
public ActionResult EditTasks(int id, FormCollection collection)
{
try
{
Week week = _model.GetWeek(id);
for (int i = 0; i < week.TaskList.Count; i++)
{
var task = week.TaskList[i];
for (int j = 0; j < task.TimeSegmentList.Count; j++)
{
int hours = Int32.Parse(collection["TaskList[" + i + "].TimeSegmentList[" + j + "].Hours"]);
week.TaskList[i].TimeSegmentList[j].Hours = hours;
}
}
//UpdateModel(week);
_model.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
查看:
<% for (int i = 0; i < Model.TaskList.Count; i++)
{
var task = Model.TaskList[i];
%>
<tr>
<td>
<%: task.Customer.CustomerName %>
</td>
<td>
<%: task.TaskName %>
</td>
<% for (int j = 0; j < task.TimeSegmentList.Count; j++)
{ %>
<td>
<%: Html.TextBoxFor(model => model.TaskList[i].TimeSegmentList[j].Hours, new { @class = "hourInput" })%>
</td>
<% } %>
</tr>
<% } %>
但是,更新必须像这样手动,这似乎不必要地复杂。我找到了Phil Haack的帖子(http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx),这似乎表明这应该可以用更简单的方式来做,如下:
[HttpPost]
public ActionResult EditTasks(Week week)
{
try
{
UpdateModel(week);
_model.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
即。简单地接收Week作为方法的参数(在Haacks示例中,它甚至看起来不必调用UpdateModel,只是将View绑定到对象似乎就足够了,所以我只需将它保存在数据库中...但它对我不起作用。返回的Week对象似乎与发送到视图的对象不同,它的任务集合中没有项目,例如
那么为什么这不起作用?
答案 0 :(得分:1)
尝试使用for
代替foreach
<fieldset>
<legend>Fields</legend>
<table width="100%">
<% for (int i = 0; i < Model.Tasks.Count; i++)
{
var task = Model.Tasks[i];
%>
<tr>
<td>
<%: task.Customer.CustomerName %>
</td>
<td>
<%: task.TaskName %>
</td>
<td>
<% for (int j = 0; j < task.TimeSegments.Count; j++)
{ %>
<%= Html.TextBox("Model.Tasks["+i+"].TimeSegments["+j+"].Hours")%>
<% } %>
</td>
</tr>
<% } %>
</table>
<p>
<input type="submit" value="Spara tid" />
</p>
</fieldset>