【问题标题】:Telerik Grid for ASP.NET MVC2 Ajax Binding doesn't work / routes to wrong actionsTelerik Grid for ASP.NET MVC2 Ajax 绑定不起作用/路由到错误的操作
【发布时间】:2011-12-19 06:14:25
【问题描述】:

网格加载服务器绑定的 b/c。 所有其他操作要么发布到错误的路线,要么发布到默认操作: 将帖子插入 /EditOrder 操作 将帖子编辑为 这个地址: http://localhost:20588/Orders/EditOrder/sdsddd?OrderID=2&CustomerID=1&ItemsInOrderGrid-mode=edit 这是没有意义的(sdsddd 是 ItemID) 没有到达控制器中 AJAX 部分内的断点。 知道我在做什么错吗?

谢谢, 丹妮

查看代码如下:

 <%=
                Html.Telerik().Grid(Model.ItemsInOrderList)
                    .Name("ItemsInOrderGrid")
                                    .DataKeys(dataKeys =>
                                    {
                                        dataKeys.Add(e => e.OrderID);
                                        dataKeys.Add(e => e.ItemID);
                                    })
                    .ToolBar(commands => commands.Insert())
                    .DataBinding(dataBinding =>
                        dataBinding.Ajax()    //Ajax binding
                .Select("ItemsGridAjax", "Orders", new {OrderID = Model.order.OrderID})
                .Insert("InsertItemsGridAjax", "Orders", new {OrderID = Model.order.OrderID})
                .Update("UpdateItemsGridAjax", "Orders")
                .Delete("DeleteItemsGridAjax", "Orders"))
                    //.BindTo(Model.ItemsInOrderList)
                    .Columns(c =>
                        {
                            c.Bound(o => o.ItemID);
                            c.Bound(o => o.OrderID).Column.Visible = false;
                            c.Bound(o => o.ItemDescription);
                            c.Bound(o => o.NumOfItems);
                            c.Bound(o => o.CostOfItem);
                            c.Bound(o => o.TotalCost);
                            c.Bound(o => o.SupplyDate);
                            c.Command(commands =>
                                {
                                    commands.Edit();
                                    commands.Delete();
                                }).Width(200);
                        })

            %>

这是控制器中的代码:

[GridAction]    
public ActionResult ItemsGridAjax(int OrderID)       
{          
return View(ordersRepository.GetOrderItemsTK(OrderID));     
}

[HttpPost]
        [GridAction]
        public ActionResult InsertItemdGridAjax(int OrderID)
        {
            //Create a new instance of the EditableCustomer class.
            ItemsInOrder newItem = ItemsInOrder.CreateItemsInOrder(OrderID, "");
            newItem.OrderID = OrderID;

            //Perform model binding (fill the customer properties and validate it).
            if (TryUpdateModel(newItem))
            {
                //The model is valid - insert the customer.
                bool res = ordersRepository.InsertItemToOrder(OrderID, newItem);
            }

            //Rebind the grid
            return View(ordersRepository.GetOrderItemsTK(OrderID));
        }



[HttpPost]
  [GridAction]
  public ActionResult UpdateItemsGridAjax(int OrderID, string ItemID)
  {
      //Find a customer whose CustomerID is equal to the id action parameter
      ItemsInOrder item = ordersRepository.FindItemByID(OrderID,ItemID);

      if (item != null)
      {
          //Perform model binding (fill the customer properties and validate it).
          if (TryUpdateModel(item))
          {
              //The model is valid - update the customer and redisplay the grid.
              ordersRepository.UpdateItem(item);
          }
      }
      // TODO: Add try-catch with error reporting.
      //Rebind the grid
      return View(ordersRepository.GetOrderItemsTK(OrderID));
  }

[HttpPost]
        [GridAction]
        public ActionResult DeleteItemsGridAjax(int OrderID, string ItemID)
        {
            //Find the customer with the specified id
            ItemsInOrder item = ordersRepository.FindItemByID(OrderID, ItemID);

            if (item != null)
            {
                //Delete the customer
                ordersRepository.DeleteItem(item);
            }

            //Rebind the grid
            return View(ordersRepository.GetOrderItemsTK(OrderID));
        }

【问题讨论】:

    标签: asp.net-mvc-2 telerik-grid


    【解决方案1】:

    我不确定您是否需要这些 Ajax 操作上的 [HttpPost] 属性(我认为只需 [GridAction] 就足够了),也许尝试删除这些属性,看看是否能解决问题。

    如果这不起作用,请尝试在您的操作中返回 GridModel,如下所示:

            [GridAction]
            public ActionResult InsertItemdGridAjax(int OrderID)
            {
                //Omitted Code
    
               return View(new GridModel(ordersRepository.GetOrderItemsTK(OrderID)));
            }
    

    您也可以使用类似于以下的语法(我认为GridModel 喜欢使用total):

            [GridAction]
            public ActionResult InsertItemdGridAjax(int OrderID)
            {
                //Omitted Code
    
                //Get List of Order Items
                List<OrderItem> list = ordersRepository.GetOrderItemsTK(OrderID));
    
                return View(new GridModel
                {
                    Data = list,
                    Total = list.Count
                });
            }
    

    【讨论】:

    • 不,没有帮助。就像“AJAX”被禁用并进入整页帖子一样。第一个 AJAX 调用 - ItemsGridAjax 没有被调用,所以我得到一个空网格。如果我通过服务器绑定填充它(如示例中所示,为网格提供 ctor 中的 ItemsInOrder 列表 - 这是唯一有效的方法,然后我可以看到编辑和删除也不起作用...... )
    • 我怀疑这会奏效 - 但值得一试。也许用 [AcceptVerbs(HttpVerbs.Post)] 替换 [HttpPost] ?不知道这是否会产生任何影响:)
    • 啊,我想我明白了——你返回的是一个视图而不是一个 GridModel——我会添加它的代码,但我认为这可能会解决它。
    • 谢谢,但没有帮助。我已经添加了它,正如我在 Telerik 的示例中看到的那样(但不是在文档中)我在所有 AJAX 函数上设置了断点,它们甚至没有到达,编辑/插入将整个页面与另一个未处理的参数:localhost:20588/Orders/…
    • 我想知道是否没有引用一些必要的 Ajax / Telerik 文件。我之前遇到过一些关于他们的一些组件的问题,几乎所有这些问题都需要我引用特定的文件。谢天谢地 - 他们的文档很棒,论坛非常快速/活跃。
    【解决方案2】:

    A 有同样的问题,但在 MVC 3 中

    解决方案只是将适当的 *.js 脚本添加到项目中,请参阅this 并将@(Html.Telerik().ScriptRegistrar().jQuery(false)) 添加到_Layout.cshtml 文件的末尾

    然后路由就好了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2018-01-01
      相关资源
      最近更新 更多