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?
-
did it successfully enter the breakpoint? if not you should try to specify controller/action through @Url.ActionDbl– Dbl2014年04月10日 13:49:00 +00:00Commented Apr 10, 2014 at 13:49
-
yes it enters the break point but postdata is always nullDRobertE– DRobertE2014年04月10日 13:51:09 +00:00Commented Apr 10, 2014 at 13:51
-
@denas yes it will be null only. your sending json data not a simple stringSivaRajini– SivaRajini2014年04月10日 13:52:51 +00:00Commented Apr 10, 2014 at 13:52
2 Answers 2
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",
2 Comments
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.
2 Comments
Explore related questions
See similar questions with these tags.