简体   繁体   中英

How to call a JQuery function from a MVC controller

Is there a way to run a jquery function from a MVC controller. I have a computer running Windows 7 Embedded and uses a touch screen without a keyboard. As part of the application, I need to copy files off of the computer. In the MVC application, I have a file transfer razor view that I want to use to select the files to copy to the usb. I have a event in the controller that fires an event (runs ac# method) when the usb is inserted or removed. I would like this event method to enable a button on the view so the user can't try to save files (the button will be disabled) until a usb is inserted. I have the code to detect the usb insertion, determine the drive letter of the usb, but I can't get it to enable the button. If I could call a jquery function from the controller, I think I can use the function to enable the button.
I have looked at “Call Jquery function from a MVC Action” on stack overflow but they have the event triggered from a form. I have an event triggered by the controller. Is there a different / better way to enable the (save) button on a view when a usb drive is inserted?

You can create new ActionResult method in your controller, which will render partial view that contains that button. So when usb is inserted, event is triggered, and in MVC controller RedirectToAction calls your new method that renders partial view with button.

I think the only way you could achieve this is by repeatedly calling a controller action through Ajax to find out if the usb is known to be available. Use setInterval to call a function every n seconds.

However, what happens when they pull it out?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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