[英]How to call both javascript function and C# function on a single button click in MVC
I want to call a javascript and a function written in Model Class using a single button click. 我想通过单击一个按钮来调用用Model Class编写的javascript和函数。 I used the following code:
我使用以下代码:
<script language="javascript" type="text/javascript">
function RunEXE() {
var txtfile = document.getElementById("txtFileName");
//var txtProgram = document.getElementById("txtProgram");
//if ((!String.IsNullOrEmpty(txtfile)) && (!String.IsNullOrWhiteSpace(txtProgram))) {
if (txtfile.value != "") {
var oShell = new ActiveXObject("WScript.Shell");
//var prog = "c:\\Pgms\\sample0.exe";
var prog = "\\\\Test-PC\\Programms\\" + txtfile.value + ".exe";
oShell.Run('"' + prog + '"', 1);
} else {
alert('The file name must be entered in file name textbox');
}
}
</script>
<input type="submit" name="button" value="Run" onclick="RunEXE()" />
The below code is Model function: 下面的代码是Model函数:
public ActionResult Run(UserProgram userProgram)
{
SaveAndCompile(userProgram);
return null;
}
But its working with Run() alone and not running RunEXE() 但是它仅与Run()一起使用而没有运行RunEXE()
[HttpPost]
public ActionResult RunAction(string option1) { //if needed, you can use the "option1" value to determine the UserProgram to pass UserProgram userProgram = new UserProgram(); public ActionResult RunAction(string option1){//如果需要,您可以使用“ option1”值来确定要传递UserProgram的UserProgram userProgram = new UserProgram(); Run(userProgram);
运行(userProgram);
//you can return a JSON reuslt that you can evaluate back at the client
return Json(new { @Success = true, @MyString = "a string" });
} }
$.post('@Url.Action("RunAction", "MyController")',
{
option1: "some optional value"
},
function (data) {
alert("success!");
//here you have access to your JSON result via data, for example:
//data.Success = true
//data.MyString = "a string"
}
); );
In your case, you can submit your form by JQuery submit function. 在您的情况下,您可以通过JQuery提交功能提交表单。
I assume your code will like below: 我假设您的代码如下所示:
<form id="form" action="/Run">
// your some inputs
<input type="submit" name="button" value="Run" />
</form>
And the javascript for submitting will be: 提交的JavaScript将是:
$(function() {
$('#form').submit(function() {
// to do something before the form is submitted
RunEXE();
return true; // return false to cancel form action
});
});
Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.