I've been puzzling over some odd behaviour with my MVC2 project for the last few nights.
I have an MVC action result that accepts a project ID and a complex Json object, looking like this:
[HttpPost]
public JsonResult AddStory(int projectid, Story story)
{
try
{
Project prj = repository.Single(p => p.ID == projectid);
//prj.Stories.Add(story);
//repository.SaveChanges();
return Json(new { Result = story });
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(new { Result = 0 });
}
}
My jQuery code for sending the complex object looks like this so far (complex object has more properties than this, but am trying only these two for debugging reasons):
$.ajax({
url: '/Project/1/AddStory',
data: { Summary: myStory.Summary, Size: myStory.Size },
dataType: 'json',
processData: false,
traditional: true,
type: 'POST'
});
My problem is that no matter how I post this object; even if I send it to a different controller and action, the request never seems to hit the server and the page is automatically redirected to the following URL:
Firebug's console shows an error in jQuery.min.js with the right headers, but no POST or RESPONSE values.
I've tried looking at the traffic in Fiddler and I can see that the request headers appear well-formed:
Accept application/json, text/javascript, /
And there is definitely an object being sent in the query string. So what am I missing? I'm sure there has to be a simple reason why things are getting so borked.
Edit:
Routes (from Global.asax) are the following plus the default mapped route:
routes.MapRoute(
"Project",
"Project/{projectid}/{action}/{id}",
new { controller = "Project", action = "Index", id = "" });