简体   繁体   English

通过AJAX发送ASP.NET模型

[英]Sending ASP.NET Model through AJAX

I'm trying to send part of a model through an ajax call, but doing it simply like my code below, doesn't work. 我正在尝试通过ajax调用发送模型的一部分,但这样做就像下面的代码一样,不起作用。 How could I pass this object along? 我怎么能传递这个对象?

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

This is what my JS puts outs: 这就是我的JS推出的内容:

MyProj.Domain.Entities.Company

This is my error: 这是我的错误:

Uncaught ReferenceError: MyProj is not defined 

Your syntax would work fine for a primitive variable, but you should serialize your object to Json before sending. 您的语法适用于原始变量,但您应该在发送之前将对象序列化为Json。 And also make sure that script stays in cshtml or aspx page, else '@Html' helper will not work. 并确保脚本保留在cshtml或aspx页面中,否则'@Html'帮助程序将无效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM