【发布时间】:2011-04-04 16:04:22
【问题描述】:
这是我的情况 -
我有两个嵌套视图模型:
<%=Html.EditorFor(x => x.DisplayEntitiesWithRadioboxesViewModel)%><br />
位于它们的父级 (StructureViewModel) 中,我可以轻松填充嵌套的 ViewModel 并将其传递给主视图:
在控制器内 - 示例
var moveDepartment = new StructureViewModel();
moveDepartment.DisplayEntitiesWithRadioboxesViewModel = fullDepartmentList.Select(x => new DisplayEntityViewModel
{
Id = x.Id,
Path = x.Path,
PathLevel = x.PathLevel,
Description = x.Description,
});
return View(moveDepartment);
EditorTemplete - 示例
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Site.Areas.Administration.ViewModel.DisplayEntityViewModel>>" %>
<table class="aligncenter"><%
if (Model != null)
{
foreach (var entity in Model)
{%>
<tr class="tRow">
<td style="text-align:left; text-indent:<%=Html.Encode(entity.PathLevel)%>em">
<%=Html.Encode(entity.Description)%>
<%=Html.RadioButton("radiobutton",entity.Id)%>
</td>
</tr><%
}
}%>
</table>
namespace Site.Areas.Administration.ViewModel
{
public class DisplayEntityViewModel
{
public int Id { get; set; }
public string Path { get; set; }
public string PathLevel { get; set; }
public string Description { get; set; }
}
}
但是,当我尝试撤回此信息时,嵌套的 ViewModel 为空:
[HttpPost]
public ActionResult Move(StructureViewModel StructureViewModel)
当我将鼠标悬停在 StructureViewModel 上时,它只包含父 ViewModel 中的数据集。例如:可以看到一个隐藏的值,但是 DisplayEntitiesWithRadioboxesViewModel = null。
我知道如何访问 DisplayEntitiesWithRadioboxesViewModel 的唯一方法是使用 FormCollection 并遍历 FormCollection 并从嵌套的 ViewModel 中提取我需要的信息。
然而,这似乎并不正确,正如我发现的那样,我必须使用 FormCollection 中的值重新填充 DisplayEntitiesWithRadioboxesViewModel,例如,如果发生错误并且用户需要发送回同一个视图。
我已尝试搜索网络/书籍,但找不到解决方案。
有没有更好的方法?
提前感谢您的帮助。
你为什么要使用 EditorFor 简单的下拉菜单,很容易 与 DropDownFor 一起使用
现在已更改为使用 DropDownFor。
什么是关键 DisplayEntitiesWithRadioboxesViewModel FormCollection 中的值
{string[3]}
[0] = "DisplayEntitiesWithRadioboxesViewModel.radiobutton"
[1] = "Action"
[2] = "OldParentId"
克莱尔:-)
【问题讨论】:
标签: asp.net asp.net-mvc-2 viewmodel