2014-01-20 72 views
0

我创建了一个电子学习系统,现在我正在尝试使用Rustici创建的Javascript库集成tincanapi,并且我想知道是否可以从MVC控制器调用JavaScript方法。在Web视图创建使用下面的代码tincan声明:在其上工作这么好,这么好一个接受好友请求按钮的点击从MVC控制器执行JavaScript函数

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

此代码调用。

但是现在我想跟踪用户上传课程的时间,当然我可以在提交表单时做到这一点,但是这使我不确定上传是否成功,所以我认为最好是如果可能的话,这些调用控制器动作。这可以做到吗?我怎么能在这个代码中调用类似的语句:

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

回答

0

你只能在返回时从控制器调用一个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);" 
} 

在这种情况下的JavaScript必须声明你正在返回的功能,还可以通过从行动的任何参数代入函数,如果是服务于您的目的。

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

1

有(现在)TinCan.NET库,在这种情况下会更好用。这样它可以直接从您的控制器调用LRS,而不是通过JavaScript从客户端进行调用。您可以找到有关该库在这里的信息:

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

对于其他库的列表,请查看:

http://tincanapi.com/libraries

+0

干杯布赖恩我意识到这一点,因为我是积极开展活动有它更新 :) – Jay

相关问题