简体   繁体   English

如何将C#代码值访问到javascript中?

[英]how to access c# code values into javascript?

I have code like following 我有如下代码

    function AddValidation() {
            var distance=@Utilities.getConfiguration(Configuration.DistanceUOM);

            $("#km").rules('add', { chekrule: ["#PerKm", "Cost Per " +distance +"should be positive."] });

}

I'm getting error for distance as undefined. 我收到distance未定义的错误。

unable to get the proper value of @Utilities.getConfiguration(Configuration.DistanceUOM); 无法获取@Utilities.getConfiguration(Configuration.DistanceUOM);的正确值@Utilities.getConfiguration(Configuration.DistanceUOM); in java script function 在Java脚本功能中

please correct me in syntax. 请纠正我的语法。

Now sure what kind of project you are using. 现在确定您正在使用哪种项目。

It could be 它可能是

function AddValidation() {
            var distance="<%=ConfigurationManager.AppSettings["DistanceUOM"] %>";
            $("#km").rules('add', { chekrule: ["#PerKm", "Cost Per " +distance +"should be positive."] });

}

OR 要么

function AddValidation() {
            var distance="@ConfigurationManager.AppSettings["DistanceUOM"]";
            $("#km").rules('add', { chekrule: ["#PerKm", "Cost Per " +distance +"should be positive."] });

}

depending on your mvc version. 取决于您的MVC版本。

Also you don't provide information what Utlilities is but if that code works in the code behind, it will also work on your view if you include the correct library at the top 另外,您没有提供什么是Utlilities信息,但是如果该代码在后面的代码中有效,则如果您在顶部包含正确的库,它也将在您的视图中起作用

<%@ Import Namespace="Ulities" %>

or 要么

 @using Ulities

So if you include that library properly then you can use your thing: 因此,如果适当地包含该库,则可以使用您的东西:

function AddValidation() {
            var distance="<%= Utilities.getConfiguration(Configuration.DistanceUOM)%>";
            $("#km").rules('add', { chekrule: ["#PerKm", "Cost Per " +distance +"should be positive."] });

}

Can't help you more without knowledge of your project 在不了解您的项目的情况下无法为您提供更多帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM