1

using the d3.js graphs, I added the graph in the View cshtml page. now i want that the values in the graph will withdraw from my DB. so I wrote the following function in the Controller:

    protected int GetReadinessAvg()
    {
        var avgReadiness = 0;
        var countItems = 0;
        foreach (var item in db.Reviews)
        {
            avgReadiness = avgReadiness + item.LecturerReadine;
            countItems++;
        }
        avgReadiness = avgReadiness / countItems;

        return avgReadiness;
    }

This function works great and really return the relevant value. Now, in the graph (the Js code), i want to use this value. Here is what I trying to do..

 var freqData = [
                 { State: '2013', freq: { LecturerReadine: '<%=GetReadinessAvg()%>', LecturerTransferRate: 412, LecturerAttitude: 674, LecturerKnowledge: 2001 } }
                , { State: '2014', freq: { LecturerReadine: 932, LecturerTransferRate: 2149, LecturerAttitude: 418, LecturerKnowledge: 4726 } }
                , { State: '2015', freq: { LecturerReadine: 832, LecturerTransferRate: 1152, LecturerAttitude: 1862, LecturerKnowledge: 2135 } }
                ];

But call to the function: LecturerReadine: '<%=GetReadinessAvg()%>' isn't working. Any suggestions?

2 Answers 2

1

As @Remy Grandin said, You can't directly call the controller method from JavaScript. But you can call C# function in your .cshtml page. Use @functions {..}

@functions{

 protected int GetReadinessAvg()
    {
        var avgReadiness = 0;
        var countItems = 0;
        foreach (var item in db.Reviews)
        {
            avgReadiness = avgReadiness + item.LecturerReadine;
            countItems++;
        }
        avgReadiness = avgReadiness / countItems;

        return avgReadiness;
    }

}

then assign value.

var freqData = [
                 { State: '2013', freq: { LecturerReadine: '@GetReadinessAvg()', LecturerTransferRate: 412, LecturerAttitude: 674, LecturerKnowledge: 2001 } }
                , { State: '2014', freq: { LecturerReadine: 932, LecturerTransferRate: 2149, LecturerAttitude: 418, LecturerKnowledge: 4726 } }
                , { State: '2015', freq: { LecturerReadine: 832, LecturerTransferRate: 1152, LecturerAttitude: 1862, LecturerKnowledge: 2135 } }
                ];
Sign up to request clarification or add additional context in comments.

Comments

0

In ASP MVC, you can't call a C# function directly. What you can do however is converting this function as an action and format a xml or json response with the controller Content() function.

With this xml or json available, you can make a simple js AJAX call and load the data into your graph.

2 Comments

ahh ok, i didn't knew it. maybe do you have a simple example of this?
An exemple : blog.kurtschindler.net/… but they are many way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.