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

  • Home
  • SEARCH
  • 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 6694425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:06:01+00:00 2026-05-26T06:06:01+00:00

I need to pass javascript object to ASP.NET MVC and I’m thinking do it

  • 0

I need to pass javascript object to ASP.NET MVC and I’m thinking do it like this:

var p = { account:'123', page:'item' }; 
var message = escape(p.toSource());
// pass message to action method

That produces something like this (unescaped for readability):

({account:"123", page:"item"})

And in ASP.NET MVC I’m trying to deserialize it and fail.
First, DataContractJsonSerializer was complaining about parenthesis, no problem with that, removed before passing:

{account:"123", page:"item"}

Then it complained about a instead of “, so I’ve tried to serialize datacontract using it, and got:

{"account":"123", "page":"item"}

So, question is, can i use something in ASP.NET MVC, that would work with javascripts toSource format, or am I doing it from scratch wrong?

  • 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-26T06:06:01+00:00Added an answer on May 26, 2026 at 6:06 am

    So, question is, can i use something in ASP.NET MVC, that would work
    with javascripts toSource format, or am I doing it from scratch wrong?

    The DataContractJsonSerializer class is pretty strict in terms of JSON format and it adheres to the specification. For example:

    {account:"123", page:"item"}
    

    is invalid JSON according to the specification. You must put double quotes around property names. You could use JSON.stringify in order to produce valid JSON:

    var p = { account:'123', page:'item' }; 
    var message = JSON.stringify(p);
    

    which will produce {"account":"123","page":"item"} which is now valid JSON. The JSON.stringify function is natively built into modern browsers and if you want to support legacy browsers you could include json2.js into your page.

    This being said, you could use the less strict JavaScriptSerializer class, or Json.NET which will accept your invalid JSON string:

    public class MyModel
    {
        public string Account { get; set; }
        public string Page { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            var json = "{account:\"123\", page:\"item\"}";
            var serializer = new JavaScriptSerializer();
            var model = serializer.Deserialize<MyModel>(json);
            Console.WriteLine("account = {0}, page = {1}", model.Account, model.Page);
        }
    }
    

    And this being said I don’t know why you are deserializing manually the JSON instead of relying on the built in JsonValueProviderFactory:

    [HttpPost]
    public ActionResult MyAction(MyModel model)
    {
        ...
    }
    

    and to invoke from javascript using AJAX:

    $.ajax({
        url: '@Url.Action("MyAction", "Home")',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ account: "123", page: "item" }),
        success: function(result) {
            // TODO: process the results
        }
    });
    

    See, now you now longer have to worry about any manual serialization/deserialization. Everything is handled by the framework for you.

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

Sidebar

Related Questions

I would like to pass a nested JavaScript object to my ASP.NET MVC Action
I need to pass an url from asp.net load_page to flowplayer javascript function here:
I need value to be passing from ASP.NET page to JavaScript and then to
I need to pass an array from JavaScript to a page method in C#.
I need to pass a javascript data to the server-side on post-back. Ex var
I'm trying to pass a string like this: {key:[value],key2:undefined,key3:undefined,key4:undefined,key5:value} to a javascript-function like this:
I need to pass a javascript object , info is a javascript object. register.php?t=+accessToken+&u=+info
I need to pass data to a variable in my master page each time
I need to pass an object (my own business object) between two tables in
I need to pass multiple arguments to a function that I would like to

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.