所有我第一次搜索我的问题,但无法找到任何帮助我进一步。ASP.NET MVC:传递一个复杂的viewmodel到控制器
我想实现一个允许我为当前用户设置权限的视图。
由于数据结构我用下面的递归类,其中每个PermissionTree-对象引用的子权限(权限在我的应用程序分层结构):
public class PermissionTree
{
public Permission Node; //the permission object contains a field of type SqlHierarchyId if that is relevant
public bool HasPermission;
public IList<PermissionTree> Children;
//i cut out the constructors to keep it short ...
}
这里是控制器的样子:
//this is called to open the view
public ActionResult Permissions()
{
//pass the root element which contains all permission elements as children (recursion)
PermissionTree permissionTree = PopulateTree();//the fully populated permission-tree
return View(permissionTree);
}
//this is called when i submit the form
[HttpPost]
public ActionResult Permissions(PermissionTree model)
{
SetPermissions(model);
ViewData["PermissionsSaved"] = true;
return View(model);//return RedirectToAction("Index");
}
在我使用的是强类型的观点是这样的:
@model PermissionTree
//....
@using (Html.BeginForm("Permissions", "Permission", null, FormMethod.Post, new { @class = "stdform stdform2" }))
{
<input name="save" title="save2" class="k-button" type="submit" />
<div class="treeview">
//i am using the telerik kendoUI treeview
@(Html.Kendo().TreeView()
.Name("Permissions")
.Animation(true)
.ExpandAll(true)
.Checkboxes(checkboxes => checkboxes
.CheckChildren(true)
)
.BindTo(Model, mapping => mapping
.For<PermissionTree>(binding => binding
.Children(c => c.Children)
.ItemDataBound((item, c) => {
item.Text = c.Node.PermissionName;
item.Checked = c.HasPermission;
})
)
)
)
好的,所以当我点击按钮时,我希望我的viewmodel被发送到用[HttpPost]
装饰的控制器动作。但是,当我调试应用程序时,收到的模型并不包含我的数据(尽管它不是null)。 有谁知道我可以如何实现我的目标,并获得整个viewmodel?
最好的问候, r3try
你在视图中有任何输入字段吗? – ken2k
不,我只使用带复选框的树形视图 – r3try
试着看看模型绑定中发生了什么:http:// stackoverflow。com/questions/4651085/best-practices-for-debugging-asp-net-mvc-binding – ngm