0



Current Scenario

I am using MVC 4, .net 4.5 on vs2012. I have an action which accepts a custom type. This custom type(model) is tightly bound to a view. I am making a POST via AJAX using JSON. Post will only post the relevant data and no form. Its content type is "application/json; charset=UTF-8". I am getting a nicely populated(read valid) model in my action.

The issue
Now I need to add a custom filter but I am unable to access the data via Request, Request.Form, Request.Param? I have been looking in System.Web.HttpContext.Current. If data is getting populated in my model, then it has to be somewhere in the request itself. I guess I am missing the finer print.

The javascript for posting data is somewhat like

$("#postData").click(function (event) {

    var savedObject = getJson(savedObject, parentContext);

    $.ajax({
        url: '/controller/action',
        contentType: 'application/json',
        dataType: 'json',
        data: savedObject,
        type: "POST",
        success: successCallBack,
        error: errorCallBack
    });
});
1
  • Can you post some of your code? Commented Jan 31, 2013 at 6:23

2 Answers 2

1

I see your stated wish of sending JSON data only. If you really need to stick to this, you can access the raw parameters via Request.InputStream.

Controller:

var input = new StreamReader(Request.InputStream).ReadToEnd();

This will get you a URL encoded string that you can manually parse.

Really, I would recommend Shahrooz's answer as the correct way to get where you want to go.

If the controller's Request.Form is not populating for your Ajax post, it is likely because you are manually serializing and sending the data to the controller with a contentType of application/json (a common scenario).

Here is an jQuery.ajax example that will NOT populate Request.Form on the controller.

    // JSON serialized form data.
    var data = {"id" : "1234", "name" : "Dave"};

    $.ajax({
        type: "POST",
        url: serviceUrl,
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(data || {}),
        success: function () { }
    });

Changing the contentType will cause the controller to process the post like a form submission.

    // Form encoded data.        
    var data = {"id" : "1234", "name" : "Dave"};
    data = $.param(data);

    $.ajax({
        type: "POST",
        url: serviceUrl,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: "json",
        data: data,
        success: function () { }
    });

You can also leave contentType undefined as application/x-www-form-urlencoded; charset=UTF-8 it is the jQuery default.

I would note that I only used $.ajax to better illustrate what is going on. You could also use $.post, though you will still need to send form encoded data

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks d12! I had to add the parameters(which I wish to use in attribute class ONLY) in my controller's action. This way they are being read from the stream and are accessible in the attribute class as well. Correct me if I'm wrong but if I post, then won't the response be rendered as a new page?
The response that is sent to the client will be defined by whatever your Action returns. It sounds like you want to return a JsonResponse. The type of response the client expects to get back from the server is specified by "dataType" parameter when you make the call.
1

I dont know what is your code but .If you remove contentType: "application/json; charset=utf-8" from your call to jQuery.ajax the default content type (form-urlencoded) will be used and the json data you have specified as your data parameter (data: { i: i, s: s, b: b }) will be mapped correctly to your action parameters....so unless you really want to send json data just remove the contentType and you will be fine.....look at this

1 Comment

1) I wish to send JSON data only and I don't want to post it in a form. 2) As I have already stated, Action parameters are being populated correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.