简体   繁体   English

将数据从视图传输到javascript的最佳方式

[英]Best way to transfer data from view to javascript

I'm working in an Asp.Net MVC 3 project where in one of my javascript file, a message is displayed to the user. 我正在Asp.Net MVC 3项目中工作,在我的一个javascript文件中,向用户显示一条消息。 This message in currently in one language but I want this message to be in multiples languages. 此消息目前使用一种语言,但我希望此消息为多种语言。 I don't want to store theses languages directly in the javascript file but instead in a database. 我不想将这些语言直接存储在javascript文件中,而是存储在数据库中。 I want to know if it possible to transfer data (string) from the view to the javascript file. 我想知道是否可以将数据(字符串)从视图传输到javascript文件。

One solution that I've found is to set data from my view in hidden fields and then extract it from my javascript file. 我发现的一个解决方案是在隐藏字段中从我的视图中设置数据,然后从我的javascript文件中提取它。 The problem with this solution is that I must put a lot of hidden fields and this is very hard to maintain. 这个解决方案的问题是我必须放置很多隐藏的字段,这很难维护。

Is there any other solution to this problem? 这个问题还有其他解决办法吗?

You can try JavaScriptModel ( http://jsm.codeplex.com ). 您可以尝试JavaScriptModel( http://jsm.codeplex.com )。

With this library you can add variables by calling the following function in your controller action: 使用此库,您可以通过在控制器操作中调用以下函数来添加变量:

this.AddJavaScriptVariable("VariableNameInJavaScript", SomeValue);

You can pass primitive types or complex types. 您可以传递基本类型或复杂类型。

Furthermore you could execute javascript functions with the following code in your controller action: 此外,您可以在控制器操作中使用以下代码执行javascript函数:

this.AddJavaScriptFunction("FunctionInJavaScript", SomeParameter, AnotherParameter);

Yes, your on the right path. 是的,你在正确的道路上。 All you need to do is hydrate your model appropriately and use Razor syntax to pass the value from the server to the client "along the following lines": 您需要做的就是适当地补充您的模型并使用Razor语法将值从服务器传递到客户端“沿着以下行”:

<script src="@Url.Content("~/scripts/yourapi.js")"></script>
<script>
    yourapi.foo('@Model.SomeValue');
</script>

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

相关问题 将数据从View传输到复杂控件的最佳方法 - Best way to transfer data from View to complex controls 将数据合同从服务转移到消费者的最佳方法 - Best way to transfer data contract from service to consumer 将数据从SqlDataReader传输到List的最佳方法是什么 <SomeClass> ? - What is the best way to transfer data from SqlDataReader into List<SomeClass>? 在SQL Server之间传输数据的最佳方法 - Best way to transfer data between SQL Servers "将数据从视图传输到控制器" - transfer data from view to controller 使用C#在MS SQL Express 05中将数据从一个数据库传输到另一个数据库的最佳方法 - best way to transfer data from one database to other in ms sql express 05 using c# 从WCF中检索数据表而不使用数据传输对象的最佳方法是什么 - what is the best way to retrieve a datatable from WCF without using Data transfer objects 将选项数据从一种形式转移回主形式的最佳方法 - Best way to transfer option data from one form back to the main form 在Xamarin中将数据从一个视图传递到另一个视图的最佳方法是什么? - What is the best way to pass data from one View to another in Xamarin? 在客户端和服务器之间传输日期时间的最佳方式(c#/javascript) - Best way to transfer datetimes between client and server (c# / javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM