0

I want to call a javascript and a function written in Model Class using a single button click. 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:

public ActionResult Run(UserProgram userProgram)
    {
        SaveAndCompile(userProgram);
        return null;
    }

But its working with Run() alone and not running RunEXE()

1
  • 1
    Your function in JavaScript will probably not be executed because when you click on a submit button, the default behavior is to cancel all pending processing in js. Is your Run method marked with HttpPost? Commented Mar 11, 2014 at 19:17

2 Answers 2

1
[HttpPost]

public ActionResult RunAction(string option1) { //if needed, you can use the "option1" value to determine the UserProgram to pass UserProgram userProgram = new UserProgram(); Run(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"
  }

);

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

Comments

0

In your case, you can submit your form by JQuery submit function.

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:

$(function() {
    $('#form').submit(function() {
        // to do something before the form is submitted
        RunEXE();
        return true; // return false to cancel form action
    });
});

Cheers.

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.