6

I want to Serialize my MVC form to JSON using JQuery and then want to Deserialize some values like value of input field at backend in C# but i am unable to serialize it in json...Please help me in this issue.following is my code.

<script type="text/javascript">
    $(function () {

        $('#btnsearch').click(function (e) {

            var searchname = $('#txtsearch').val();

            var form = $(this).serializeArray();

            DrawTable(form);
        });



        function DrawTable() {
            var props = [];
            props.push({ name: "FirstName", value: firstname });
            BindDataTable({ AllowPaging: true, ShowFilter: false, ShowEditLink: true, EmptyTableText: 'No Data Found', SortIndex: 0, SortDirection: "asc" },
                              "#tblCustomers",
                              "@Url.Action("GetAllCustomers", "Customer")",
                              props,
                              [{ name: "Id", cellClass: "alignCenter", Sortable: true, index: 0 }, { name: "FirstName" }, { name: "ABN" }, { name: "Phone" }, { name: "Email" }, { name: "Address1" }, { name: "City" }, { name: "Country" }],
                              [{ name: "Id", type: "anchor", title: 'customerTable', viewtitle: 'View', link: '@Url.Action("Edit", "Customer")', index: 0 }]);

        } 

       // DrawTable(data);
        //$('#myInputTextField').on('keyup', function () {
        //    oTable.search($(this).val()).draw();
        //});



    });

        </script>
ARC
  • 1,061
  • 14
  • 33

2 Answers2

5

Yes, it's a very old question and there is a lot of similar questions with answers:

But this is asking specifically for Asp.MVC: I have tested most of the answers and they fail to serialize forms encoded the way Asp.mvc works, when there are property of list type that Asp.MVC forms encodes as

   TheProperty[1].SubProperty=Value
   TheProperty[2].SubProperty=Value

The only serializer that treats that case correctly is this, when configured with the option

{ associativeArrays: false }

(thanks raphaelm22 for your solution!)

DaniCE
  • 2,412
  • 1
  • 19
  • 27
  • Been searching for this for hours, thank you! Posting ASP.NET form arrays to a JSON endpoint is impossible without this. – Codemunkie Jan 29 '20 at 10:49
0

You cannot use $(this).serializeArray(); because this refers to $('#btnsearch'), which is not a form.

Use $("#your_form_id).serializeArray(); or $("#your_form_id).serialize().

kamil-mrzyglod
  • 4,948
  • 1
  • 20
  • 29
  • can you please tell me that how to deserialize it in c# and get specific value from it like i want to get text that i entered in textbox – ARC Aug 04 '15 at 06:51
  • Or better: `$(this).closest("form").serializeArray()`, this way, you don't need to know (or even have) the id of the form. – Najkin Aug 04 '15 at 06:51
  • @AhmadCheema - there are a lot of good answers on StackOverflow how to deserialize JSON to strongly typed object. – kamil-mrzyglod Aug 04 '15 at 06:54
  • @Riokmij after serializing i got this: form = "__RequestVerificationToken=tYY7hJDUAgtisu8r8dB4VxwFhJb2m8JRPz_bIgbmJhgZjLQp6Sf-lnQE2x3TG9BNCc4B-PTPAU9TewJNUP_d6kgK8TUIej2fiPkWAiImu_Y1" is it serialized correctly ? – ARC Aug 04 '15 at 06:55