2

首先,我搜索了我的问题,但找不到任何可以帮助我更进一步的东西。

我正在尝试实现一个允许我为当前用户设置权限的视图。

作为数据结构,我使用以下递归类,其中每个 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 ...
}

这是控制器的样子:

//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;
                })

                )
            )
      )

好的,所以当我单击按钮时,我希望将我的视图模型发送到用[HttpPost]. 但是当我调试应用程序时,接收到的模型并没有真正包含我的数据(虽然它不是空的)。有谁知道我如何实现我的目标并获得整个视图模型?

最好的问候, r3try

4

2 回答 2

2

我认为最好在这里使用 JSON 帖子,然后很容易在 javascript 端准备对象。

我不知道您的 HTML 的外观或元素的名称,您可以轻松地使用 javascript/Jquery 来构建具有相似名称和更纤细的层次结构/数据类型的客户端 json 对象,就像在PermissionTree类中一样。然后使用 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) {   
               }
);   

重要的是您需要找到一种更好的方法来遍历树视图并在 javascript 中构建对象。

于 2013-01-18T10:31:54.133 回答
0

因为我无法让它工作,所以我尝试了一种稍微不同的方法:

添加节点的示例: - 按添加按钮 -> 执行 ajax 调用 -> 在 nhibernate 中添加节点 -> 使用新数据再次调用视图(包括新节点)

ajax 请求调用的控制器动作:

[Authorize]
    [HttpPost]
    public ActionResult AddPermission(string parentPermissionName, string permissionName)
    {
        var pd = ServiceContext.PermissionService.permissionDao;
        Permission parentPermission = pd.GetPermissionByName(parentPermissionName);
        if (parentPermission == null) {
            parentPermission = pd.GetRoot();
        }

        if (parentPermission != null && !string.IsNullOrEmpty(permissionName) && !pd.PermissionExists(permissionName))//only add with a name
        {
            pd.AddPermission(parentPermission, permissionName);
        }
        //refresh data
        PermissionTree permissionTree = LoadTreeSQLHierarchy(null, false);//start at root
        return View("Permissions", permissionTree);
    }

视图中的 Ajax 请求:

function addNode() {
    //... get the data here
    var addData = { parentPermissionName: selectedNodeName, permissionName: newNodeName };

    $.ajax(
       {
           data: addData,
           type: "POST",
           url: '@Url.Action("AddPermission", "Permission")',
           dataType: "json",
           success: function (result) {
               //$('.centercontent').html(response);//load to main div (?)
               return false;
           },
           error: function (xhr, ajaxOptions, thrownError) {
               alert(xhr.status + ":" + thrownError);
               return false;
           }
       }
    );
    return false;
}

But when i execute this i get an error stating that json.parse hit an invalid character (i get this error at the alert in the ajax's error function). Judging from that message i would say that the problem is that i am returning html but the ajax call expects json or so... But what is the correct way to just reload my view with the new data? Can i somehow tell the ajax call to not go back at all and just execute the called controller-method?

于 2013-01-22T08:04:34.523 回答