开发者

How to simply output serialized form back as data in AJAX form in ASP.NET MVC3

Disclaimer: I am a total noob when it comes to ASP.NET.

I am using the following jQuery code to submit my form:

            $.ajax({
                url: '/Foo/Save',
                type: 'POST',
                data: $('#dataInputForm').serialize(),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    alert(data.success);
                },
                error: function () {
                    alert("error");
                }
            });

My Foo controller code is this:

    [HttpPost]
    public ActionResult Save()
    {
        return Json(new { success = true });
    }

I know the AJAX post is working because I am seeing the alert with "success". Next, I would like to see a var dump somehow of my serialized form. I honestly do not know where to begin on the controller end but I would like my success alert to just dump my serialize form data just so that I can confirm that the form data that was submitted.

Essentially, this is what I would like the success alert to look like:

a=1&b=2&c=3&d=4&e=5

BONUS: I would love to see an alternative way of submitting the data in JSON and s开发者_如何转开发eeing the success alert in JSON.


firstly, .serialize() does not produce json, so the specified contentType is wrong. you can drop the contentType option and $.ajax() will use the default 'application/x-www-form-urlencoded'.

next, on the controller, you need to bind the posted data to some object. if you don't have an object defined that models your form data, you can use a FormCollection to build your result:

[HttpPost]
public ActionResult Save(FormCollection collection)
{
    var result = new {Foo1 = collection["Foo1"], Foo2 = collection["Foo2"]};

    return Json(result);
}

in your success handler, you can then display the properties:

success: function(data) {
    alert(data.Foo1);
    alert(data.Foo2);
}

or convert the object to a json string using this utility like so:

success: function(data) {
    alert(JSON.stringify(data));
}

for further reference:

convert json object to string


You might have to change the method signature of the method so that it is of time Stream. Then you'll have to parse through the stream as a string and split the arguments by & (ampersands) where you'll have different KVPs.

public Stream samplePost(Stream stream)
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
        StreamReader sr = new StreamReader(stream);
        string stringStream = sr.ReadToEnd();
        sr.Dispose();
        NameValueCollection kvp = HttpUtility.ParseQueryString(stringStream);
        var number1 = Convert.ToInt32(kvp["number1"]);
        var number2 = Convert.ToInt32(kvp["number2"]);
        var sum = number1 + number2;

        byte[] byteArray = Encoding.ASCII.GetBytes("SUM IS " + sum.ToString());
        MemoryStream s = new MemoryStream(byteArray);
        return s;
    }

That's a sample for taking in two numbers and returning them as a sum. I think this should set you on your way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜