从 jquery 脚本中的 @Model 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5770578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get value from @Model inside jquery script
提问by petro
How can I get value from @Model
inside jquery script. I want to get some property by index(determined by row selection in my custom table) from my Model which is IEnumerable<T>
. I don't want to show this property in table and do something like cell .val()
如何从@Model
jquery 脚本中获取价值。我想从我的模型中通过索引(由我的自定义表中的行选择确定)获取一些属性,即IEnumerable<T>
. 我不想在表格中显示此属性并执行诸如单元格之类的操作.val()
for example :
例如 :
var selectedRow = $(this).parent().children().index($(this)) - 1;
and I want something like
我想要类似的东西
@Model.ElementAt(selectedRow).SomeProperty
inside script
内部脚本
Thanks
谢谢
回答by Max Strini
@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
@Model 是一个 .NET 对象(服务器端),您的 JQuery 脚本在客户端运行并对 JavaScript 对象进行操作。您无法从客户端代码直接访问服务器端 .NET 对象 - 您需要对模型进行一些 JSON 序列化(或者可能只是您感兴趣的属性)。然后在脚本中你可以做类似的事情
var model = @Html.Raw(Json.Encode(Model))
to get your model into a JavaScript variable, then access everything through "model".
将您的模型放入 JavaScript 变量,然后通过“模型”访问所有内容。
回答by Alexander Taran
use html5 data attributes in your view.. to make your model available in html then access them via js
在您的视图中使用 html5 数据属性.. 使您的模型在 html 中可用,然后通过 js 访问它们