MVC 4.0 and JQuery .ajax
-
I'm trying to do a post back to a controller with
$.ajax
and all I get in the controller isnull
for my object. Here's my setup for the class:public class ApplicationLinkViewModel : MappableModel
{
public string ApplicationLinkId { get; set; }public string ApplicationId { get; set; } public string DisplayText { get; set; } public string Link { get; set; } }
And for the controller:
[HttpPost]
public JsonResult SaveLink(ApplicationLinkViewModel link)
{
var responseModel = new ResponseModel();
return this.Json(responseModel);
}As you can see I'm doing nothing in the controller right now, I just have two lines so I can set a breakpoint and check out the
link
variable which is always null. The JQuery function is :$.ajax({
type: "POST",
cache: false,
dataType: 'json',
url: "Item/SaveLink",
data: JSON.stringify({ ApplicationId : "1", DisplayText: "test", Link : "linked", ApplicationLinkId: "2" }),
contentType: 'application/json; charset=utf-8',
success: function(data) {
if (data.Success) {
window.searchApplications();
window.showDetails(applicationId);
}
},
error: function(data) {
alert("Error occurred : \n\n" + data.Message);
}
});I've tried several other approaches, and nothing I do seems to work, the object passed into my controlled is always null. Does anyone have any ideas? For what it is worth I am (stuck) using IE8. EDIT: SOLVED I'm going to go drown myself now. The problem was the name of my parameter "link". It must be reserved or something even though the code never complained. At any rate I changed my Controller to this and it works now:
public JsonResult SaveLink(ApplicationLinkViewModel linkViewModel
-
I'm trying to do a post back to a controller with
$.ajax
and all I get in the controller isnull
for my object. Here's my setup for the class:public class ApplicationLinkViewModel : MappableModel
{
public string ApplicationLinkId { get; set; }public string ApplicationId { get; set; } public string DisplayText { get; set; } public string Link { get; set; } }
And for the controller:
[HttpPost]
public JsonResult SaveLink(ApplicationLinkViewModel link)
{
var responseModel = new ResponseModel();
return this.Json(responseModel);
}As you can see I'm doing nothing in the controller right now, I just have two lines so I can set a breakpoint and check out the
link
variable which is always null. The JQuery function is :$.ajax({
type: "POST",
cache: false,
dataType: 'json',
url: "Item/SaveLink",
data: JSON.stringify({ ApplicationId : "1", DisplayText: "test", Link : "linked", ApplicationLinkId: "2" }),
contentType: 'application/json; charset=utf-8',
success: function(data) {
if (data.Success) {
window.searchApplications();
window.showDetails(applicationId);
}
},
error: function(data) {
alert("Error occurred : \n\n" + data.Message);
}
});I've tried several other approaches, and nothing I do seems to work, the object passed into my controlled is always null. Does anyone have any ideas? For what it is worth I am (stuck) using IE8. EDIT: SOLVED I'm going to go drown myself now. The problem was the name of my parameter "link". It must be reserved or something even though the code never complained. At any rate I changed my Controller to this and it works now:
public JsonResult SaveLink(ApplicationLinkViewModel linkViewModel
For what it is worth changing the definition of my controller method to the following works.
public JsonResult SaveLink(string ApplicationLinkId, string ApplicationId, string DisplayText, string Link)