Neo
Neo

Reputation: 16219

how to access c# code values into javascript?

I have code like following

    function AddValidation() {
            var [email protected](Configuration.DistanceUOM);

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

}

I'm getting error for distance as undefined.

unable to get the proper value of @Utilities.getConfiguration(Configuration.DistanceUOM); in java script function

please correct me in syntax.

Upvotes: 2

Views: 59

Answers (1)

Nick
Nick

Reputation: 2907

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.

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

<%@ 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

Upvotes: 3

Related Questions