首先,我搜索了我的问题,但找不到任何可以帮助我进一步了解的内容。
我正在尝试实现一个允许我为当前用户设置权限的 View 。
作为数据结构,我使用以下递归类,其中每个 PermissionTree-Object 引用子权限(权限在我的应用程序中是分层结构的):
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 ...
}
Controller 如下所示:
//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");
}
我正在使用这样的强类型 View :
@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;
})
)
)
)
好的,所以当我单击按钮时,我希望将我的 View 模型发送到用 [HttpPost]
装饰的 Controller 操作。但是当我调试应用程序时,收到的模型并不真正包含我的数据(尽管它不为空)。
有谁知道我如何实现我的目标并获得整个 View 模型?
最诚挚的问候, r3尝试
最佳答案
我认为这里最好使用 JSON post,然后很容易在 javascript 端准备对象。
我不知道你的 HTML 是什么样子,也不知道元素的名称,你可以轻松地使用 javascript/Jquery 构建具有相似名称和更精简的层次结构/数据类型的客户端 json 对象,就像 PermissionTree
中一样。类(class)。然后使用Ajax post以JSON形式发布
var PermissionTree={Node:{},HasPermission:false,Children:{}}
$.ajax({ data:PermissionTree
type: "POST",
url: 'YourController/Permissions',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
);
重要的是您需要找到一种更好的方法来浏览 Treeview 并在 JavaScript 中构建对象。
关于c# - ASP.NET MVC : passing a complex viewmodel to controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14396781/