ASP.NET MVC:将复杂的viewmodel传递给控制器

时间:2013-01-18 10:21:20

标签: c# asp.net-mvc kendo-ui model-binding kendo-treeview

所有我搜索过我的问题,但找不到任何有助于我进一步发展的事情。

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

作为我在递归类之后使用的数据结构,其中每个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;
                })

                )
            )
      )

好的,所以当我点击按钮时,我希望将我的viewmodel发送到用[HttpPost]修饰的控制器动作。但是当我调试应用程序时,收到的模型并不真正包含我的数据(尽管它不是空的)。 有谁知道我如何实现我的目标并获得整个视图模型?

最好的问候, r3try

1 个答案:

答案 0 :(得分:0)

因为我无法上班,所以我尝试了一种稍微不同的方法:

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

ajax请求调用的controller-action:

[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;
}

但是当我执行此操作时,我得到一个错误,指出json.parse命中了一个无效字符(我在ajax的错误函数中的警报中得到此错误)。 从该消息判断我会说问题是我正在返回html但是ajax调用期望json左右...... 但是用新数据重新加载我的视图的正确方法是什么?我可以以某种方式告诉ajax调用根本不回去并执行被调用的控制器方法吗?

相关问题