在我的视图中显示的模型已被用户操作过滤,因此一些原始数据不再存在。注重对ActionLink
为EditRow
,这是怎么了我的视图显示模式:如何将模型传递回ASP.NET MVC中的Controller?
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.User.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.Email)
</td>
<td>
@item.Weekly.WeekBeginning.ToShortDateString() - @item.Weekly.WeekEnding.ToShortDateString()
</td>
<td>
@(item.WeeklyTargetId != ViewBag.Edited ? Html.DisplayFor(modelItem => item.Hours) : Html.EditorFor(modelItem => item.Hours))
</td>
<td>
//here is the line I am trying to pass the model through:
@Html.ActionLink("Edit", "EditRow", new {id = item.WeeklyTargetId, currentSelection = Model}) |
@Html.ActionLink("Details", "Details", new {id = item.WeeklyTargetId}) |
@Html.ActionLink("Delete", "Delete", new {id = item.WeeklyTargetId})
</td>
</tr>
}
在ActionLink
为EditRow
我能够很容易地通过传递每个项目的ID,但是当我加入currentSelection = Model
甚至= Model.ToList()
,控制方法接收的0
这里计数是我使用的操作签名:
//I've tried IQueryable<WeeklyTarget> as well
public ActionResult EditRow(int? id, List<WeeklyTarget> selection)
我的问题是我怎么能传递立方米rrent(过滤)模型在视图中显示给控制器动作?我应该以不同的方式来解决这个问题吗?
你正在以一种不寻常的方式去解决它。通常,您的“编辑”操作采用某种类型的ID,并返回从该ID生成的模型。然后,“编辑”的POST操作将采用更新后的模型。 – Jonesopolis
我用'EditRow'替换了'Edit'动作,我打算更新这个视图中的Hours列而不是导航到新的页面。这工作正常,但是当我点击这个按钮模型恢复到未经过滤的视图。我希望数据持续存在。 –
在视图“currentSelection”应该是“选择”为了匹配您的行动方法中的参数。只是我做了类似的事情。 – asunrey