I have a question about reading a JSON string in C#. My scenario is following.
I have ASP.NET MVC C# project(.NET framework 3.5). In LobbyController I have
.
.
.
using System.IO;
using System.Web.Script.Serialization;
.
.
.
[HttpPost]
public ActionResult SomeMethod(string sampleData)
{
//do stuff here
}
and in jquery script file I have defined click function
$("#buttonID").click(function() {
var sampleData = {
"property1": $('#elementID1').val(),
"property2": $('#elementID2').val(),
"property3": $('#elementID3').val()
};
$.ajax({
url: "/Lobby.aspx/SomeMethod",
type: "POST",
data: sampleData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function() { $('#message').html('Success').fadeIn(); },
error: function() { $('#message').html('Error').fadeIn(); }
});
});
Now, on click ajax function kicks in and SomeMethod is called in controller as expected but sampleData parameter is null. I also tried modifiing "data" line in ajax function like this:
data: JSON.stringify(sampleData),
but it didn't work as well.
I tried to alert individual properties of sampleData object and they had value they're supposed to have but for some reason ActionMethod's sampleData parameter is null. Could someone tell me what I'm missing? Maybe it's a syntax error on my part or there is something that needs to be done on c# side?
Any help would be appreciated