Execute a javascript function from MVC controller

13k Views Asked by At

I have created an eLearning system and I am now trying to integrate tincanapi using the Javascript library that Rustici have created and I am wondering if it is possible to call a javascript method from the MVC controller. In the web view I create tincan statements using the following code:

<script type="text/javascript" src= "@Url.Content("~/Scripts/tincan.js")"></script>
var tincan = new TinCan
(
    {
        recordStores: [
            {
                endpoint: "https://cloud.scorm.com/tc/V4FF9VBCSY/",
                username: "myusername",
                password: "mypassword"
            }
        ]
    }
);
function AcceptFriend(fullName,emailAddress)
{
    tincan.sendStatement
    (
        {
            actor: {
                name: "@Model.User.Forename" + " @Model.User.Surname",
                mbox: "@Model.User.Email"
            },
            verb: {
                id: "http://adlnet.gov/expapi/verbs/answered",
                display: {
                    "en-US": "accepted a friend request from"
                }
            },
            target: {
                objectType: "Agent",
                name: fullName,
                mbox: emailAddress
            }
        }
    );
};

This code is called upon click of an accept friend request button which is working so well and so good.

But now I want to track when a user uploads a course, of course I can do this on the submission of the form but this leaves me unsure of whether the upload was successful so I thought that it would be best to make these calls on the controller action if possible. Can this be done? How could I call similar statements to those above within this code:

public ActionResult NewCampaign()
    {
        evm.GetCampaignTypes();
        evm.GetCampaignFormats();
        evm.GetCampaignTemplates();

        //Set ViewBag  values.
        ViewBag.UserID = evm.User.UserID;
        ViewBag.NewMessageCount = evm.NewMessageCount;
        ViewBag.PendingFriendRequests = evm.PendingFriendRequests;
        ViewBag.NewFriendRequest = evm.NewFriendRequest;
        ViewBag.NewFriendCount = evm.NewFriendCount;
        ViewBag.UserForename = evm.User.Forename;
        return View(evm);
    }
2

There are 2 best solutions below

0
On

You can only call a Javascript function from the controller upon return. For example:

JavascriptResult MyAction()
{
    // Declare the purpose of the action


    // The Javascript function
    return JavaScript("YourJavaScriptFunction");

    // If you need parameters passed
    // return JavaScript(String.Format("YourFunction({0},{1}), paramter1, parameter2);"
}

In this instance the javascript must declare the function you are returning, additionally you can pass any parameters from the action into the function if that serves your purpose.

If you need to bind data from the client to the controller you are best to use AJAX calls.

1
On

There is (now) a TinCan.NET library that would be better to use in this case. That way it can make a call from your controller directly to the LRS rather than doing it from the client side in JavaScript. You can find information about that library here:

http://rusticisoftware.github.io/TinCan.NET/

For a list of other libraries check out:

http://tincanapi.com/libraries