且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 jquery 脚本中的 @Model 获取值

更新时间:2023-02-23 22:28:59

@Model 是一个 .NET 对象(服务器端),您的 JQuery 脚本在客户端运行并对 JavaScript 对象进行操作.您不能从客户端代码直接访问服务器端 .NET 对象 - 您需要对模型进行一些 JSON 序列化(或者可能只是您感兴趣的属性).然后在脚本中,您可以执行类似

@Model is a .NET object (server-side), your JQuery scripts are running client-side and operate on JavaScript objects. You can't directly access server-side .NET objects from client-side code - you'll need to have some JSON serialization of your model (or maybe just the properties you're interested in). Then inside a script you can do something like

var model = @Html.Raw(Json.Encode(Model))

将您的模型放入 JavaScript 变量中,然后通过模型"访问所有内容.

to get your model into a JavaScript variable, then access everything through "model".