从 MVC 控制器执行 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21237748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:26:57  来源:igfitidea点击:

Execute a javascript function from MVC controller

javascriptjqueryasp.net-mvc-4controllertin-can-api

提问by Jay

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:

我已经创建了一个电子学习系统,我现在正在尝试使用 Rustici 创建的 Javascript 库来集成 tincanapi,我想知道是否可以从 MVC 控制器调用 javascript 方法。在 Web 视图中,我使用以下代码创建 tincan 语句:

<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);
    }

回答by Brian J. Miller

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:

有(现在)一个 TinCan.NET 库,在这种情况下会更好地使用。这样它就可以从您的控制器直接调用 LRS,而不是在 JavaScript 中从客户端调用。您可以在此处找到有关该库的信息:

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

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

For a list of other libraries check out:

有关其他库的列表,请查看:

http://tincanapi.com/libraries

http://tincanapi.com/libraries

回答by IsakBosman

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

您只能在返回时从控制器调用 Javascript 函数。例如:

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.

在这种情况下,javascript 必须声明您要返回的函数,此外,如果符合您的目的,您可以将任何参数从操作传递到函数中。

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

如果您需要将数据从客户端绑定到控制器,您最好使用 AJAX 调用。