1

I'm just trying to pass a simple object to an MVC controller method without having to create a view model for it. The parameter in the controller method is always null though it id getting called. My jquery post is below.

 $(function () {
    $("button[id$='addidentifier']").click(function (evt) {
        alert('button click');
        evt.preventDefault();
        var postdata = {
            identifier: $('#Identifier').val(),
            goldkey: $('#Goldkey').val()
        };
        $.ajax({
            type: 'POST',
            url: 'altbloomberg/AddIdentifier',
            data: JSON.stringify({ postdata :  postdata }) ,
            success: function(data) {
                 alert('data: ' + data);
            },
            failure: function(a,b,c) {
                alert(a);
            },
            //contentType: "application/json",
            dataType: 'json'//,
            //processData: false
        });
    });

the controller method is simple enough, I don't even have the body of it done cause I wanted to get the data passing in first.

    [System.Web.Http.HttpPost]
    public ActionResult AddIdentifier(string postdata)
    {
           // put here to place a breakpoint to look at postdata
           string lookhere = "";
    }

I've looked at other post and this is what they "seem" to indicate to do. This seems like it should be easy enough. So what am I doing wrong here?

Per some suggestions below: I changed the jquery function to pass

         $.ajax({
            type: 'POST',
            url: 'altbloomberg/AddIdentifier',
            contentType: "application/json",
            dataType: 'json',
            data: JSON.stringify(postdata),
            success: function(data) {
                 alert('data: ' + data);
            },
            failure: function(a,b,c) {
                alert(a);
            }
        });

I just changed the stringify method params and set the content type, then changed the controller params to

    (string identifier, string goldkey).  

This works as the results of the stringify method are

   "{"identifier":"XS0939678792","goldkey":"LAW"}"

So why is it then if I just use ONE param in the controller method

   (String postdata) 

and use

   JSON.Stringify({postdata:postdata}) 

the results of which are

  "{"postdata":{"identifier":"XS0939678792","goldkey":"GOVT"}}" does it NOT work. 

Shouldn't the controller method match on the postdata json value?

DRobertE
  • 3,478
  • 3
  • 26
  • 43

2 Answers2

3

I think that you have to create a simple model to receive data.

For example, this is your action:

[System.Web.Http.HttpPost]
public ActionResult AddIdentifier(PostDataModel postdata)
{

}

where PostDataModel is a simple model with this structure:

class PostDataModel {
     public string Identifier {get;set;}
     public string GoldKey {get;set;}
}

and you have to enable this line in your ajax request:

contentType: "application/json",
Ragnar
  • 4,393
  • 1
  • 27
  • 40
  • that's what I'm trying to avoid, cause then every time i add a new json property I have to edit the model, THEN edit the controller. – DRobertE Apr 10 '14 at 14:14
  • i think it depends of your final goal, you can use PostDataModel as a DTO, in this way you only have to add new properties to PostDataModel and pass PostDataModel to other methods as a parameter – Ragnar Apr 10 '14 at 14:27
2

The parameters for AddIdentifier action should be either a model with the 'identifier' and 'goldkey' properties or a string parameter for each of those properties.

Like this:

[System.Web.Http.HttpPost]
public ActionResult AddIdentifier(Identifier model)

or this:

[System.Web.Http.HttpPost]
public ActionResult AddIdentifier(string identifier, string goldkey)

If you really want the stringified data then you can take dataType:'json' out and it will just send the string.

Edit

Based on your comments it seems you want something more dynamic. You could change the type of your postdata parameter from string to dynamic. I'm not sure if this will work out of the box or not. If it doesn't you can create your own custom model binder to bind the data to a dynamic parameter.

http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder

If I were you I would stick to one of the first two options of creating a model or defining the parameters, though.

jdehlin
  • 10,909
  • 3
  • 22
  • 33
  • Kinda want to have the easier of the two worlds, I don't want to have to create a Model "Identifier" or others for each controller method and I don't want to have to add a new param to the controller signature every time I add a new param in the json object. I just want to match at the top level json object as I've edited above – DRobertE Apr 10 '14 at 14:07
  • Ended up just using a model. Dynamic didn't work out of the box and no point in making a custom... drat – DRobertE Apr 11 '14 at 13:02