Search code examples
asp.netajaxasp.net-mvc-4routeattribute

ASP.NET MVC Controller - 500Error


Why isn't my controller function called? I always get 500error (in fiddler). I get no error in Visual Studio or an error site.

Controller:

[POST("/test1")]  // attributerouting (works with GET methods)
public ActionResult test1(TreeViewItemModel aItem)
{
  ...
}

Client:

var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
        type: "POST",
        url: "/test1",
        data: tree,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        }
    });

Solution

  • problem was the data format: solution:

    public ActionResult test1(IEnumerable<TreeViewItemModel> aItem)
    {
    }
    

    Client:

    var tree = $("#demo2").jstree("get_json");
    var c = JSON.stringify(tree);
     $.ajax({
        type: "POST",
        url: "/test1",
        data: c,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        }
    });