DirectMethod - how to pass an array parameter

  1. #1

    DirectMethod - how to pass an array parameter

    Hi guys,

    Let's say I want to call a DirectMethod on my JS client, passing an array of strings as one of the parameters. Something like:

    App.direct.MyMethod(["arrayValue1","arrayValue2"]);
    I assumed a corresponding server-side method that receives a string array would would work and I'd get my array. However, what I actually get an array with only one element - the client-side array as a string.

    [DirectMethod]
    public ActionResult MyMethod(string[] arrayParameter) {
       // By inspecting arrayParameter, we get [ "[\"arrayValue1\",\"arrayValue2\"]" ]
       ...
    }
    I found a workaround, which involves receiving the array as a string and deserializing it:

    [DirectMethod]
    public ActionResult MyMethod(string arrayParameterAsString) {
       var myArray = JsonConvert.DeserializeObject<string[]>(arrayParameterAsString);
       
       // Do stuff with parameters
       ...
    }
    But my gut feeling tells me there's a simpler way to do this. Is there some way to tell Ext.NET that I want a parameter to be treated as an array on my server method?

    Thanks in advance!
  2. #2
    Hi @tcunha,

    Welcome to the Ext.NET forums!

    ASP.NET MVC manages deserialization, we don't control it.

    You can use a ModelBinder attribute.
    public ActionResult TestArray([ModelBinder(typeof(JsonModelBinder))]string[] array)
  3. #3
    Try to use Ext.encode
  4. #4
    Thanks, @Daniil! The ModelBinder did the trick!

Similar Threads

  1. DirectMethod cant Register my Array
    By kmlplt in forum 1.x Help
    Replies: 7
    Last Post: Nov 12, 2012, 8:55 PM
  2. [CLOSED] How to pass parameter when using HttpWriteProxy
    By Daly_AF in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 16, 2012, 9:46 PM
  3. Pass Parameter to store at runtime
    By Vaishali in forum 1.x Help
    Replies: 0
    Last Post: Feb 07, 2012, 6:58 AM
  4. How to pass object as a parameter?
    By diegovbs in forum 1.x Help
    Replies: 1
    Last Post: Jan 04, 2011, 5:53 PM
  5. [CLOSED] To pass an Array using ajaxmethod
    By speedstepmem3 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 31, 2009, 7:58 AM

Posting Permissions