Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
290
IgGrid - Updating URL posting - application/x-www-form-urlencoded instead of JSON
posted

Hi,

When the saveChanges event is fired and the UpdateUrl of my grid is aclled, the data is posted using application/x-www-form-urlencoded instead of application/json

Thus, the ModelBinder with JSOn cannot Workd properly.

Moreover, the date is posted with the format /Date(1398988800000)/ so when I deserialize the response it fails whend trying to convert as dateTime.

Any idea ?

Thanks

Parents
  • 23953
    Offline posted

    Hello PEO-Stokomani,

    igGrid uses transaction objects on the client to persist user changes. This transaction objects have a corresponding model class (Infragistics.Web.Mvc.Transaction<T>) in the Infragistics.Web.Mvc.dll which we use to deserialize the data sent from the grid by using another method Infragistics.Web.Mvc.GridModel.LoadTransactions<T> from the library. This solution doesn't use the ASP.NET MVC ModelBinder.

    Here is an example code on how to implement this solution (the code was taken from the Basic Editing sample):

    Code Snippet
    1. public ActionResult OrdersSaveData()
    2. {
    3.     GridModel gridModel = new GridModel();
    4.     List<Transaction<Order>> transactions = gridModel.LoadTransactions<Order>(HttpContext.Request.Form["ig_transactions"]);
    5.     var orders = RepositoryFactory.GetOrderRepository();
    6.     foreach (Transaction<Order> t in transactions)
    7.     {
    8.         if (t.type == "newrow")
    9.         {
    10.             orders.Add(t.row);
    11.         }
    12.         else if (t.type == "deleterow")
    13.         {
    14.             orders.Delete(o => o.OrderID == Int32.Parse(t.rowId));
    15.         }
    16.         else if (t.type == "row")
    17.         {
    18.             var order = (from o in orders.Get()
    19.                             where o.OrderID == Int32.Parse(t.rowId)
    20.                             select o).Single();
    21.             if (t.row.OrderDate != null)
    22.             {
    23.                 order.OrderDate = t.row.OrderDate;
    24.             }
    25.             if (t.row.TotalPrice != null)
    26.             {
    27.                 order.TotalPrice = t.row.TotalPrice;
    28.             }
    29.             if (t.row.TotalItems != null)
    30.             {
    31.                 order.TotalItems = t.row.TotalItems;
    32.             }
    33.             if (t.row.CustomerID != null)
    34.             {
    35.                 order.CustomerID = t.row.CustomerID;
    36.             }
    37.             if (t.row.ShipAddress != null)
    38.             {
    39.                 order.ShipAddress = t.row.ShipAddress;
    40.             }
    41.             orders.Update(order, o => o.OrderID == Int32.Parse(t.rowId));
    42.         }
    43.     }
    44.     orders.Save();
    45.     JsonResult result = new JsonResult();
    46.     Dictionary<string, bool> response = new Dictionary<string, bool>();
    47.     response.Add("Success", true);
    48.     result.Data = response;
    49.     return result;
    50. }

    Hope this helps,
    Martin Pavlov
    Infragistics, Inc. 

Reply
  • 23953
    Verified Answer
    Offline posted in reply to PEO-Stokomani

    Hello PEO-Stokomani,

    You can still use Infragistics.Web.Mvc.GridModel.LoadTransactions<T>  method to do the heavy lifting in your model binder.

    Here is an example code:

    Code Snippet
    1. public class InfraSingleTransactionGenericModelBinder: IModelBinder
    2. {
    3.     public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    4.     {
    5.         GridModel model = new GridModel();
    6.         Type tGridModel = model.GetType();
    7.         MethodInfo mLoadTransactions = tGridModel.GetMethod("LoadTransactions");
    8.         MethodInfo gmLoadTransactions = mLoadTransactions.MakeGenericMethod(bindingContext.ModelType);
    9.         Type transactionType = typeof(Transaction<>);
    10.         Type gTransaction = transactionType.MakeGenericType(bindingContext.ModelType);
    11.         var l = gmLoadTransactions.Invoke(model, new object[] { controllerContext.HttpContext.Request.Form["ig_transactions"] });
    12.         FieldInfo pi = gTransaction.GetField("row");
    13.         return pi.GetValue(((IList)l)[0]);
    14.     }
    15. }

    Remember that in your controller method you need to return JsonResult with the correct Data.Success=true property so that the igGrid can commit the transaction on the client side.

    Hope this helps,
    Martin Pavlov
    Infragistics, Inc. 

Children
No Data