Telerik griglia per ASP.NET Ajax MVC2 Binding non funziona / rotte per azioni sbagliate

StackOverflow https://stackoverflow.com/questions/4669890

  •  10-10-2019
  •  | 
  •  

Domanda

I carichi gird b / c di binding del server. Tutte le altre azioni sono o annuncio alla strada sbagliata, o per l'azione di default: Inserire messaggi all'azione / EditOrder Modifica i messaggi a questo indirizzo: http: // localhost: 20588 / Ordini / EditOrder / sdsddd IDOrdine = 2 & CustomerID = 1 & ItemsInOrderGrid-mode = modifica che è privo di significato (sdsddd rappresenta l'ItemID) non dei punti di interruzione all'interno delle sezioni AJAX nel controllore vengono raggiunti. Qualche idea di cosa sto facendo di sbagliato?

Grazie, Dani

ecco il codice vista:

 <%=
                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);
                        })

            %>

Ecco il codice nel controller:

[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));
        }
È stato utile?

Soluzione

Non sono sicuro se è necessario l'attributo [HttpPost] (penso appena [GridAction] sarà sufficiente) su quelle azioni Ajax, forse provare a rimuovere quelle e vedere se questo risolve il problema.

Se questo non funziona, provare a restituire un GridModel nelle vostre azioni in questo modo:

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

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

Si potrebbe anche usare la sintassi simile al seguente (come penso che il GridModel piace avere un 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
            });
        }

Altri suggerimenti

A aveva lo stesso problema, ma in MVC 3

La soluzione era solo per aggiungere script appropriato * .js nel progetto, vedere questo e aggiungere @(Html.Telerik().ScriptRegistrar().jQuery(false)) nella fine del file _Layout.cshtml

A allora il routing va bene!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top