0

I am trying to bind a JSON stringified object to a model and few other strings in the controller and it is not working.

is it not possible,

$.ajax({
url: "/SrcManager/AddDataSource",
type: "POST",
data: JSON.stringify({
    content: ct,
    dataSourceName: $("#dataSrcName").val(),
    parameters: parametersCollection,
    sourceContentId: sourceContentId,
    sourceId: null,
    type: contType
}),
success: function (data) {
    if (data.length > 1)
    {
        alert("DataSource Saved Successfully");
        $("#dataSrcId").val(data);
    }
}
});

and var parametersCollection = [];

function IPParameters(paramName, paramValue) { this.ParamName = paramName; this.ParamValue = paramValue; }

*** action method : public string AddDataSource(ContentModel scvm){.........}

Why does the above not work. Is this not supported or any mistake in the code, kindly suggest the right way.

In the ContentModel, i map the parameters to List<Parameters>.

I have added the JsonValueProviderFactory also in global.asax.

The C# model is :

public class SourceContentViewModel
{
    public string sourceId { get; set; }

    public string dataSourceName { get; set; }

    public string sourceContentId { get; set; }

    public string content { get; set; }

    public string type { get; set; }
    public List<Parameters> parameters { get; set; }

    public SourceContentViewModel()
    {
        parameters = new List<Parameters>();
    }
}

public class Parameters
{
    public string ParamName { get; set; }
    public string ParamValue { get; set; }
}
3
  • Can you show your action method and the javascript you use to send that data? Commented Dec 27, 2011 at 9:49
  • @Jan: i have updated the question itself Commented Dec 27, 2011 at 9:54
  • Can you show how the ContentModel class is defined? Your question is about modelbinding. Commented Dec 27, 2011 at 9:56

1 Answer 1

2

You can't send JSON to an ASP.NET MVC 2 application as there is no JSON provider factory out-of-the-box that will allow you to read the request. It is built in ASP.NET MVC 3. You may take a look at the following blog post and include the JsonValueProviderFactory discussed there. Then you will be able to send JSON requests to your ASP.NET MVC 2 controller actions after registering it:

protected void Application_Start() 
{
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}

Also notice the contentType: 'application/json; charset=utf-8' setting when sending the request which instructs the binder that you are sending a JSON request.

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

5 Comments

Pls take a look at the updated question. this was working fine sometime back.. but something messed up now..
@saravanan, no, it's impossible that this has ever worked in an ASP.NET MVC 2 application. Out of the box it doesn't support JSON requests. You are using JSON.stringify in your AJAX request which serializes the object into a JSON string. Maybe before you didn't have this JSON.stringify call?
Adding this to the ajax request fixed the issue. contentType: 'application/json; charset=utf-8'. Thanks Darin Dimitrov.
@saravanan, if this is working in an ASP.NET MVC 2 application it is because you must have registered a JsonValueProviderFactory as shown in my answer.
I did register the JsonValueProviderFactory in the global.asax. you are right. but forgot the contenttype in ajax call.

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.