从@Model得到jQuery脚本内在价值 [英] get value from @Model inside jquery script
问题描述
我怎样才能从 @Model
jQuery脚本内在价值。我想通过索引(按行选择在我的自定义表中确定)从我的模型的一些属性,它是的IEnumerable< T>
。我不想在显示表中的属性,并完成类似的细胞 .VAL()
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()
例如:
var selectedRow = $(this).parent().children().index($(this)) - 1;
和我想要的东西像
@Model.ElementAt(selectedRow).SomeProperty
里面的脚本
感谢
推荐答案
@Model是一个.NET对象(服务器端),您的JQuery脚本运行的客户端和JavaScript对象进行操作。你不能直接访问从客户端code服务器端.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".
这篇关于从@Model得到jQuery脚本内在价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!