从jquery脚本中的@Model获取值

时间:2022-03-21 10:24:55

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()

如何从jquery脚本中的@Model获取值。我希望通过我的模型中的索引获取一些属性(由我的自定义表中的行选择确定),即IEnumerable 。我不想在表中显示此属性并执行类似单元格.val()的操作

for example :

例如 :

var selectedRow = $(this).parent().children().index($(this)) - 1;

and I want something like

我想要类似的东西

 @Model.ElementAt(selectedRow).SomeProperty

inside script

Thanks

2 个解决方案

#1


35  

@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变量,然后通过“模型”访问所有内容。

#2


-1  

use html5 data attributes in your view.. to make your model available in html then access them via js

在视图中使用html5数据属性..使您的模型在html中可用,然后通过js访问它们

#1


35  

@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变量,然后通过“模型”访问所有内容。

#2


-1  

use html5 data attributes in your view.. to make your model available in html then access them via js

在视图中使用html5数据属性..使您的模型在html中可用,然后通过js访问它们