用于ASP.NET MVC2的Telerik Grid Ajax绑定不会对错误的操作起作用/路由

时间:2011-01-12 14:27:56

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

gird加载服务器绑定的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));
        }

2 个答案:

答案 0 :(得分: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
            });
        }

答案 1 :(得分:1)

A在MVC 3中有同样的问题

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

然后路由就好了!