最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Execute a javascript function from MVC controller - Stack Overflow

programmeradmin1浏览0评论

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: "/",
                username: "myusername",
                password: "mypassword"
            }
        ]
    }
);
function AcceptFriend(fullName,emailAddress)
{
    tincan.sendStatement
    (
        {
            actor: {
                name: "@Model.User.Forename" + " @Model.User.Surname",
                mbox: "@Model.User.Email"
            },
            verb: {
                id: "",
                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);
    }

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./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);
    }
Share Improve this question asked Jan 20, 2014 at 15:25 JayJay 3,08216 gold badges53 silver badges105 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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./libraries

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.

发布评论

评论列表(0)

  1. 暂无评论