简体   繁体   English

如何从MVC控制器调用JQuery函数

[英]How to call a JQuery function from a MVC controller

Is there a way to run a jquery function from a MVC controller. 有没有一种方法可以从MVC控制器运行jquery函数。 I have a computer running Windows 7 Embedded and uses a touch screen without a keyboard. 我有一台运行Windows 7 Embedded的计算机,并且使用不带键盘的触摸屏。 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. 在MVC应用程序中,我有一个文件传输剃刀视图,我想用它来选择要复制到USB的文件。 I have a event in the controller that fires an event (runs ac# method) when the usb is inserted or removed. 我在控制器中有一个事件,当插入或移除USB时会触发一个事件(运行ac#方法)。 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. 我希望此事件方法在视图上启用一个按钮,以便用户无法尝试保存文件(该按钮将被禁用),直到插入USB。 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. 我有检测USB插入的代码,确定USB的驱动器号,但是我无法通过它启用按钮。 If I could call a jquery function from the controller, I think I can use the function to enable the button. 如果我可以从控制器调用jquery函数,我想可以使用该函数来启用按钮。
I have looked at “Call Jquery function from a MVC Action” on stack overflow but they have the event triggered from a form. 我已经看过堆栈溢出时的“从MVC操作调用Jquery函数”,但是他们从表单触发了事件。 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? 插入USB驱动器时,是否有其他/更好的方法在视图上启用(保存)按钮?

You can create new ActionResult method in your controller, which will render partial view that contains that button. 您可以在控制器中创建新的ActionResult方法,该方法将呈现包含该按钮的局部视图。 So when usb is inserted, event is triggered, and in MVC controller RedirectToAction calls your new method that renders partial view with button. 因此,当插入USB时,会触发事件,并在MVC控制器中,RedirectToAction调用您的新方法,该方法使用按钮呈现部分视图。

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. 我认为,实现此目标的唯一方法是通过Ajax反复调用控制器操作,以找出USB是否已知可用。 Use setInterval to call a function every n seconds. 使用setInterval每n秒调用一次函数。

However, what happens when they pull it out? 但是,当他们将其拔出时会发生什么?

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

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