Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

asp.net mvc - Post multiple parameters to MVC Controller using C#

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);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...