所有我第一次搜索我的問題,但無法找到任何幫助我進一步。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