2011-01-12 35 views
4

網格負載b/c服務器綁定。 所有其他行動要麼張貼到了錯誤的路線,或默認動作: 插入員額/ EditOrder行動 編輯職位,以 這個地址: http://localhost:20588/Orders/EditOrder/sdsddd?OrderID=2&CustomerID=1&ItemsInOrderGrid-mode=edit 這是沒有意義的(sdsddd是項目ID) 非的控制器中AJAX部分內的斷點到達。 任何想法我做錯了什麼?Telerik Grid for ASP.NET MVC2 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)); 
     } 

回答

1

我不知道,如果你需要的[HttpPost]屬性(我認爲只需[GridAction]就足夠了),也許嘗試刪除這些並查看是否可以解決問題。

如果沒有工作,試圖返回在你的行動GridModel像這樣:

 [GridAction] 
     public ActionResult InsertItemdGridAjax(int OrderID) 
     { 
      //Omitted Code 

      return View(new GridModel(ordersRepository.GetOrderItemsTK(OrderID))); 
     } 

你也可以使用類似於以下(因爲我覺得有totalGridModel喜歡)語法:

 [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 
      }); 
     } 
+0

nope,沒有幫助。這就像「AJAX」被禁用,它會發送到整頁文章。第一個AJAX調用 - ItemsGridAjax不會被調用,所以我得到一個空的網格。如果我通過服務器綁定(給網格中的ItemsInOrder在列表中的例子) - 這是唯一的作品,然後我可以看到,編輯和刪除不起作用以及... ) – Dani 2011-01-12 14:46:59

1

一個有同樣的問題,但在MVC 3

解決的辦法是剛剛加入適當的* .js文件的腳本我nto項目,請參閱this 並將@(Html.Telerik().ScriptRegistrar().jQuery(false))添加到_Layout.cshtml文件末尾

然後路由進行得很順利!