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 6102527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:37:27+00:00 2026-05-23T13:37:27+00:00

I am posting a json object to an ASP.Net MVC controller with C# code.

  • 0

I am posting a json object to an ASP.Net MVC controller with C# code. To keep things simple in this example the object is just a car with make and model properties. Everything works well with the code below. My question is – how would I post multiple parameters? For example, how would I post a JSON object, an email address, and a phone number?

    //post to form
    string requestData = "{\"Make\":\"Ford\",\"Model\":\"Mustang\"}";
    byte[] data = Encoding.UTF8.GetBytes(requestData);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://receiving.url/showdata");
    request.Method = "POST";
    request.ContentType = "application/json";
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(data, 0, data.Length);
    dataStream.Close();

    WebResponse response = request.GetResponse();
    string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
    Console.Write(result);

Here is the Controller code that retrieves the json object from the post, and then outputs the json for verification purposes.

    [HttpPost]
    public JsonResult showdata(Car c)
    {
        return Json(c, JsonRequestBehavior.AllowGet);
    }

I’m looking to do something like this:

    [HttpPost]
    public JsonResult showdata(Car c, string email, string phone)
    {

        return Json(c, JsonRequestBehavior.AllowGet);
    }
  • 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-23T13:37:28+00:00Added an answer on May 23, 2026 at 1:37 pm

    Like this:

    string requestData = "{\"c\": {\"Make\":\"Ford\",\"Model\":\"Mustang\"}, \"email\": \"foo@bar.com\", \"phone\": \"1111\"}";
    

    Or even better using a JavascriptSerializer:

    using System;
    using System.Net;
    using System.Text;
    using System.Web.Script.Serialization;
    
    class Program
    {
        static void Main()
        {
            var serializer = new JavaScriptSerializer();
            string requestData = serializer.Serialize(new
            {
                c = new
                {
                    make = "Ford",
                    model = "Mustang"
                },
                email = "foo@bar.com",
                phone = "1111"
            });
    
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var result = client.UploadData("http://receiving.url/showdata", Encoding.UTF8.GetBytes(requestData));
                Console.WriteLine(Encoding.UTF8.GetString(result));
            }
        }
    }
    

    which will take care of properly JSON serializing your object as if you are using those string concatenations your request might break easily if the data contains some special characters.

    Oh, and before I forget: use view models.

    So instead of:

    [HttpPost]
    public JsonResult showdata(Car c, string email, string phone)
    {
        ...
    }
    

    you should definitely be having:

    [HttpPost]
    public ActionResult ShowData(ShowDataViewModel data)
    {
        ...
    }
    

    and then:

    string requestData = serializer.Serialize(new
    {
        make = "Ford",
        model = "Mustang",
        email = "foo@bar.com",
        phone = "1111"
    });
    

    And another remark: you don’t need JsonRequestBehavior.AllowGet when returning your JSON as you have decorated your controller action with [HttpPost] so this action can never be invoked with a GET verb.

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

Sidebar

Related Questions

I am posting json data to the asp.net server page along with the $.post
When using the Ajax.BeginForm() helper in ASP.Net MVC , I can pass options with
After posting this question and reading that one I realized that it is very
before posting the whole code, i wanted to make sure I am not missing
I'm posting this in the spirit of answering your own questions. The question I
ok, I'm back. I totally simplified my problem to just three simple fields and
HI, I'm having a JSON parsed return object set. { word:[ offered, postings ],
I'm trying to load JSON back into an object. The loads method seems to
I have an interesting situation that has me stumped. It seems that posting appliction/json
I'm trying to parse JSON string using Boost Spirit store JSON object into recursive

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.