Submitting ASP.NET Model via AJAX

I am trying to send part of the model via an ajax call, but this is just like my code below, doesn't work. How can I pass this object?

$.ajax({
            url: "/Controller/Action",
            type: "GET",
            data: @Model.Company,
            success: function (result) {
            $('#myDiv').html(data);
        }
});

      

This is what my JS puts out outs:

MyProj.Domain.Entities.Company

      

That's my fault:

Uncaught ReferenceError: MyProj is not defined 

      

+3


source to share


1 answer


Your syntax will work fine for a primitive variable, but you need to serialize your object to Json before submitting. Also, make sure the script stays on the cshtml or aspx page and the '@Html' helper won't work.



$.ajax({
            url: "/Controller/Action",
            type: "GET",
            data: @Html.Raw(Json.Encode(Model.Company)),
            success: function (result) {
            $('#myDiv').html(data);
        }
});

      

+7


source







All Articles