Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6620721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:08:22+00:00 2026-05-25T21:08:22+00:00

The goal here is to have MVC framework recognize and populate a field in

  • 0

The goal here is to have MVC framework recognize and populate a field in my action that is a FormCollection object. I can get it to work if I handle all the serialization myself but so far no dice if I leave it to jquery/javascript.

Question: What is causing the following array to be serialized as if it contained no data?

I checked the variable and it contains one item just as expected (set elsewhere in code).

    var formVars = new Array();
    for (var item in gd["FormVariables"])
    {

        if (gd["FormVariables"][item] != null)
        {

            formVars[item.toString()] = gd["FormVariables"][item];
         }
     }
    var args = {
        Req: {
            SelectedColId: gd["SelectedColId"],
            CurrentPage: gd["CurrentPage"],
            PageSize: gd["PageSize"],
            RecordCount: gd["RecordCount"],
            NumberOfPages: numOfPages,
            MultipleSelection: gd["MultipleSelection"],
            FormVariables: formVars
        }
    };

The issue with this is the behavior of the array … if I do an alert(formVars); it appears empty even though it contains all the key/value pairs. Likewise, the FormVariables parameter is empty after JSON.stringify() does its work.

 $.ajax(
            {
                traditional: true,
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                url: gd["Route"],
                data: JSON.stringify(args),
                success: function (data, textStatus, jqXHR)
                {
                },
                error: function (jq, status, err)
                {
                    alert(status);
                }
            });

}

serialized output:

'{"Req":  
    {
       "SelectedColId":"",
       "CurrentPage":1,
       "PageSize":15,
       "RecordCount":0,
       "NumberOfPages":2,
       "MultipleSelection":false,
       "FormVariables":[]
     }
}'

what is going wrong here?

EDIT: gd contents

{
      Height: 300, 
      FetchType: 1, 
      Route: '../GetTrainingDocuments/', 
      SelectedColId: '', 
      PageSize: 15, 
      CurrentPage: 1, 
      RecordCount: 0, 
      HasMoreRecords: true, 
      NumberOfPages: 1, 
      MultipleSelection: false, 
      FormVariables: function() 
      {
          if (this.array == 'undefined') 
          {
             this.array = [];
          } 
          return this.array;
      }
}

The Model it should bind to:

public ActionResult GetTrainingDocuments(GridRequest req)
{
   //...
}

public class GridRequest
{
    public string SelectedColId { get; set; }
    public int CurrentPage { get; set; }
    public int PageSize { get; set; }
    public int RecordCount { get; set; }
    public int NumberOfPages { get; set; }
    public string Widths { get; set; }
    public bool MultipleSelection { get; set; }
    public FormCollection FormVariables { get; set; }

    public GridRequest()
    {           
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:08:23+00:00Added an answer on May 25, 2026 at 9:08 pm

    You have declared the formVars variable as array an yet here: formVars[item.toString()] you are trying to address it by some value that is probably not an integer. In javascript array must have 0 based integer indexes. So instead of:

    formVars[item.toString()] = gd["FormVariables"][item];
    

    if you want to use an array you need:

    formVars.push(gd["FormVariables"][item]);
    

    or if you wanted to preserve the item as key:

    formVars.push({ key: item, value: gd["FormVariables"][item] });
    

    If you want FormVariables to be an object (i.e. an associative array in javascript) then:

    var formVars = {};
    for (var item in gd["FormVariables"])
    {
        if (gd["FormVariables"][item] != null)
        {
            formVars[item.toString()] = gd["FormVariables"][item];
        }
    }
    

    It really depends on what view model you are trying to bind this on the server side.


    UPDATE:

    Now that you have posted your gd variable, looking at the FormVariables property, it looks very strange:

    FormVariables: function() {
        if (this.array == 'undefined') {
            this.array = [];
        } 
        return this.array;
    }
    

    This will always return an empty array which limits the usefulness of such structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So my goal here is to have a single search field in an application
I have a project here the goal is to merge multiple Access DB's into
For example, my goal is to test the code given here: PHP script that
My goal here is to have the browser download a csv file using headers
I am using asp.net mvc. I have generated a view that retrieve all unapproved
I'm trying to ensure that visitors of my ASP.NET MVC website always have the
I have standart MVC web project that register routes in global.asax. My MVC project
The overall goal here is to have jetty be configured with a client certificate
I have an MVC 2 app that I want all requests to return json.
The goal here is have my complete database configuration and schema generation handled by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.