0

I need to call a JavaScript function in a Controller function. 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!

3
  • 1
    Why do you need to call a client side function from the server side? What are you trying to achieve? Commented Sep 28, 2011 at 20:23
  • 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. Commented Sep 28, 2011 at 20:30
  • Please let me know if what I am trying to do is bad practice! I would love to adopt a better way to hook up my map (JavaScript) with my database. Commented Sep 28, 2011 at 20:34

2 Answers 2

1

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

You need to poll the server with Ajax, use sockets (very limited support), or comet to ask the server for new data. The server can not just send down the data at will.

Sign up to request clarification or add additional context in comments.

4 Comments

I have the updates in a JSON object. And updateMap() reads that JSON. Now I want to call updateMap() in the controller (I used to be a PHP programmer). The Polling seems interesting. I am going to read it now.
Another reason I want to call updateMap on database update is that I don't want to redraw my map every time I send a request to the server. I am sure there is a way to detect the rows changed. But I will probably just end up redrawing every row in the database since I am a lazy person.
You need to make a request to the server to detect that rows have changed. That is what stackoverflow does to detect if people posted answers while you are sitting on the page.
Websockets? Comet? Dude, get with the program.
0

I would recommend looking into 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.