2013-01-18 89 views
2

所有我第一次搜索我的问题,但无法找到任何帮助我进一步。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

+0

你在视图中有任何输入字段吗? – ken2k

+0

不,我只使用带复选框的树形视图 – r3try

+0

试着看看模型绑定中发生了什么:http:// stackoverflow。com/questions/4651085/best-practices-for-debugging-asp-net-mvc-binding – ngm

回答

2

我觉得这是更好地在这里使用一个JSON后,则很容易在JavaScript方面准备的对象。

我不知道HTML的外观如何,或者元素的名称可以很容易地使用javascript/Jquery构建客户端json对象,类似名称和更简单的层次结构/数据类型,就像在PermissionTree类中一样。然后使用Ajax后张贴的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构建的对象。

+0

我想我没有完全理解这个...... 你的意思是当我点击提交按钮时,我应该使用javascript手动创建序列化对象,然后使用ajax将数据发布到我的控制器?我真的不明白为什么在mvc中存在强类型视图时,甚至无法将该视图模型传递回控制器。是不是修改模型的观点? .. \ *叹* \ *沮丧 - 谢谢你的回复! ;) – r3try

+1

问题在于您拥有的模型集合。要强烈发布类型视图并填充模型,您需要在视图中使用适当的命名约定。我的意思是在HTML中,你的输入应该有像'Children [0] .Chilren [1] .Permission'这样的名字。 –

+0

如果您没有那种最好的方法,那就是填充JSON对象并使用Ajax传递它。 –

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请求在View:

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调用不回去,只需执行调用的控制器方法?

相关问题