Reputation: 3
I'm trying to get some JSON functionality to work, it's basically as simple as it can be but for some reason I'm getting a 500 Internal server error on my console and the application never reaches the controller. I've tried just about everything, but the code is so simple I've given up trying to find the error by readin the same couple of lines over and over.
Here is my script:
$("#saveAdvisorOption").click(function () {
// Create a JSON object:
alert("OK");
var OptionsModel = { "AdvisorNewsFeed": $("#advisorCheckBox").val() };
alert($(OptionsModel.AdvisorNewsFeed));
$.post("/Account/ChangeOptionForAdvisor", OptionsModel, function (data) {
});
});
Here is my model:
public class OptionsModel
{
public string AdvisorNewsFeed { get; set; }
public OptionsModel(string AdvisorNewsFeed)
{
this.AdvisorNewsFeed = AdvisorNewsFeed;
}
}
}
And here is my controller:
public class AccountController : Controller
{
[HttpPost]
public ActionResult ChangeOptionForAdvisor( OptionsModel option)
{
//Return something
return Json(option, JsonRequestBehavior.AllowGet);
}
I have a break point on the return statement but it is never run.
Any help is greatly appreciated.
Upvotes: 0
Views: 2373
Reputation: 28703
You probably just need to add an empty constructor to the OptionsModel
class. MVC can't instantiate the class without an empty constructor.
Upvotes: 3
Reputation: 150253
@scottm got the major problem.
That said:
You probably need to tell jQuery you're expecting to get a json
object:
$.post("/Account/ChangeOptionForAdvisor", OptionsModel, function (data) {...});
To this:
$.post("/Account/ChangeOptionForAdvisor", OptionsModel, function (data) {
...}, 'json'); //<======
Upvotes: 1