简体   繁体   English

从控制器调用javascript函数

[英]call javascript function from controller

I need to call a JavaScript function in a Controller function. 我需要在Controller函数中调用JavaScript函数。 My code looks like the following: 我的代码如下所示:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(DeviceLocation locationToEdit)
    {
        var originalLocation = (from m in _db.DeviceLocations
                                where m.Id == locationToEdit.Id
                                select m).First();

        if (!ModelState.IsValid)
            return View(originalLocation);

        _db.ApplyCurrentValues(originalLocation.EntityKey.EntitySetName, locationToEdit);
        _db.SaveChanges();

        Utility.mostRecentLocationUpdate = locationToEdit;

        /******************************************
        need to call updateMap() JavaScript function
        *******************************************/

        return RedirectToAction("Locations");
    }

Thanks! 谢谢!

I need to update a map when a database changes. 当数据库更改时,我需要更新地图。 Ideally, the updateMap() should be a callback function that gets executed automatically when a row in the Location table changes 理想情况下,updateMap()应该是一个回调函数,当Location表中的一行更改时,该函数将自动执行

You need to poll the server with Ajax , use sockets (very limited support), or comet to ask the server for new data. 您需要使用Ajax轮询服务器 ,使用套接字 (非常有限的支持)或彗星请求服务器提供新数据。 The server can not just send down the data at will. 服务器不能随便发送数据。

I would recommend looking into SignalR . 我建议调查SignalR It allows the sort of functionality you want, but it unfortunately doesn't have very good documentation, and Google is always trying to correct your searches to "signal" which is annoying when trying to find information on it. 它可以提供您想要的某种功能,但是很遗憾,它没有很好的文档,并且Google一直试图将搜索结果更正为“信号”,这在尝试查找有关信息时很烦人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM