How to inject JavaScript variable into | vue js url action
User-155375195postedHi,IamnewtoMVC.Followingcodeworks:
Hi, I am new to MVC. Following code works:
<script type="text/javascript"> $(document).ready(function () { $("#myBtn").click(function () { window.location = "@Url.Action("Index", "Home", new { msg = "Hello" })"; }); }); </script>The problem in the above example is, msg is hard-coded to be "Hello". The business logic requires it to be a JavaScript variable that is returned by another function:
<script type="text/javascript"> $(document).ready(function () { $("#myBtn").click(function () { var vMsg = getMsg(); window.location = "@Url.Action("AnotherPage", "Home", new { msg = vMsg })"; }); }); </script>But compiler will throw error: "The name vMsg does not exist in the current context"
I know why, because it is looking for a vMsg variable in @{ ... } and could not find it. But...