Editor send [object + Object] instead of data to the Controller

Editor send [object + Object] instead of data to the Controller

ecodaecoda Posts: 6Questions: 1Answers: 0

I have a very simple Editor:

    var editor = new $.fn.dataTable.Editor({
        ajax: '/FinObj/FinObj',
        table: '#tblFinObj',
        fields: [...]
    });

When I confirm the Update the HttpContext.Request.Form in my MVC Controller contains:

action: "edit" (string)
data: "[object+Object]" (string)

I expect that "data" value is the model object, not a string.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Try:

    ajax: {
      url: '/FinObj/FinObj',
      data: function (d) {
        return JSON.stringify(d);
      }
    }
    

    Beyond that, if that doesn't help, can you link to your page so I can take a look and see what it is submitting to the server please?

    Thanks,
    Allan

  • ecodaecoda Posts: 6Questions: 1Answers: 0

    Good morning Allan,
    thanks for your soon reply.

    I modified the view accordingly to your advice, but the issue hasn't disappeared (but changed)

    Ajax send this JSON string:

    "{\"action\":\"edit\",\"data\":{\"369\":{\"ID\":\"369\",\"InternalCode\":\"02607\",\"Name\":\"PRIVATE EQUITY OPPORTUNITIES\",\"ShortName\":\"PRIVATE EQUITY O\",\"ISIN\":\"\",\"BBFigiCode\":\"\",\"BBTicker\":\"\",\"Currency\":\"EUR\",\"PortCode\":\".FND2607_\",\"IsExtraBB\":1,\"DeletionDate\":\"\"}}}"

    In the Controller the HttpContext.Request.Form contains only 1 Key and its value is null (v. image)

    Going on the controller fails in editor.Process(formData) with "Object reference not set to an instance of an object." exception.

    Attached Index.cshtml file (the view) and the FinObjController.cs

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Oh I see - you are using our .NET libraries. In that case the above change I suggested shouldn't be required (in fact it will stop things working).

    If you remove it, what is the JSON returned by the server please?

    When I confirm the Update the HttpContext.Request.Form in my MVC Controller contains:

    That shouldn't be a problem as long as the object can be resolved. Its not clear from the above if that is the case or not.

    Allan

  • ecodaecoda Posts: 6Questions: 1Answers: 0

    Hi Allan,
    yes, I'm using your .NET libraries.

    When the (MVC) Controller execute
    editor.Process(formData);

    I got this Exception:
    "Method not found: 'System.String[] System.String.Split(Char, System.StringSplitOptions)'."

    the StackTrace:
    at DataTables.DtRequest.HttpData(IEnumerable`1 dataIn)
    at DataTables.DtRequest._Build(IEnumerable`1 rawHttp)
    at DataTables.DtRequest..ctor(IEnumerable`1 rawHttp)
    at DataTables.Editor.Process(NameValueCollection data)
    at K2PortWeb.Controllers.FinObjController.FinObj() in E:\Sviluppo Key2Port\Key2PortAgent\K2PortWeb\Controllers\FinObjController.cs:line 92

    Regards

  • ecodaecoda Posts: 6Questions: 1Answers: 0

    UPDATE

    I replaced the DataTables-Edito-Server.dll v. 1.8.0 (from Generator project) with v. 1.9.0 (from NuGet) and now on the modal form bar appear:
    "Object reference not set to an instance of an object."

    this error seems coming from editor.Data() in Controller

  • ecodaecoda Posts: 6Questions: 1Answers: 0

    UPDATE (2)

    To bypass the problem I send form data by JSON:

        var editor = new $.fn.dataTable.Editor({           
            ajax:
            {
                url: '/FinObj/FinObj',
                type: "POST",
                contentType: "application/json",
                data: function (d) {
                    var ret = JSON.stringify(d);
                    return ret;
                }
            },
    

    In the MVC Controller I wrap data to model and then I convert the model to a NameValueCollection in order to process by editor.Process() method.

    This solution has some issue, i.e. Bubble editing is not permitted, as Ajax return all view field, not only the changed field

    Then I created a project by your Generator tool.
    It works good, but it's different from my solutions:
    - it use Web API framework instead of MVC
    - the view is "pure" HTML and not Razor view

    Do you think these differences could matter?

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Answer ✓

    It is possible that using MVC as you have requires the data in a slightly different form. It really depends how MVC is setup and what version you are using.

    Regarding the bubble editing, that's a client-side construct, so it shouldn't make any difference if you are using bubble editing or main form editing.

    Allan

  • ecodaecoda Posts: 6Questions: 1Answers: 0

    Hi Allan,

    you're right, bubble editing return only the changed field, it was my mistake as I wrapped the result to the model.

    Now I've created a class to cath the JSON response of the editor and convert it to a NameValueCollection for Editor.Process() method.

    Thanks

This discussion has been closed.