从MVC中的控制器调用Javascript函数时出错 [英] Error while calling Javascript function from controller in MVC

查看:50
本文介绍了从MVC中的控制器调用Javascript函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MVC控制器调用Javascript函数,



这就是我用来调用



I am calling a Javascript function from MVC Controller,

This is what i am using to call

public ActionResult Index(string name, string email, string address)
        {
            ViewBag.Name = name;
            JavaScript("myfunction('123')");
            return View(ViewBag);
        }







和javascript我写的这个






and in javascript i have written this

<script>
    $(document).ready(
       function myfunction(e) {
           alert('Hello Test' + e);
       }
        );
</script>





当我运行它时,它会显示以下意外结果



Hello Testfunction(e,t){return new x.fn.init(e,t,r)}



And When i run it, it shows following unexpected result

"Hello Testfunction(e,t){return new x.fn.init(e, t,r)}"

推荐答案

document )。ready(
function myfunction(e){
alert (' Hello Test' + e);
}
);
< / script>
(document).ready( function myfunction(e) { alert('Hello Test' + e); } ); </script>





当我运行它时,它会显示以下意外结果



Hello Testfunction(e,t){return new x.fn.init(e,t,r)}



And When i run it, it shows following unexpected result

"Hello Testfunction(e,t){return new x.fn.init(e, t,r)}"


你不能打电话当控制器在生成html的服务器上执行时,来自控制器的javascript,然后发送到浏览器执行。您的.net代码没有在浏览器内部运行,因此无法与DOM交互。



其次,您之所以看到了什么'在警告框中重新查看是因为您正在定义一个在文档准备就绪时调用的函数,即所有内容都已加载并且您希望运行jQuery函数。您定义的函数接受一个参数并提醒它,因此当jQuery决定DOM已加载时,它会调用您的函数(将其Init事件作为参数传递),并且您的函数在警报中显示该函数。当您提醒某个功能时,您会看到该功能中的代码。



第三,这不是您使用JavaScript方法的方式,它是一种真正适合的返回类型如果要将一些js传递给客户端脚本以调用成功,则通过AJAX调用的操作。如果你确实想要使用它,你可以做类似的事情;



You can't call javascript from your controller as the controller executes on the server which generates html which is then sent to the browser to execute. Your .net code isn't "running inside" the browser so it can't interact with the DOM.

Secondly, the reason you're seeing what you're seeing in the alert box is because you are defining a function that is called when the document is "ready", ie everything has loaded and you want your jQuery functions to run. The function you defined accepts a param and alerts it, so when jQuery decides the DOM has loaded it calls your function (passing its Init event as the parm) and your function is showing that in an alert. When you alert a function you see the code in the function.

Thirdly, that's not how you use the JavaScript method, it is a return type that is really intended for actions called via AJAX if you want to pass some js to the client script to call on success. If you did want to use it you could do something like;

public ActionResult Index()
{
    ViewBag.js = JavaScript("myfunction('123')");
    return View();
}







<script>


(document).ready( function(){
function myfunction(e){
alert('Hello Test'+ e);
}

@ Html.Raw(ViewBag.js。脚本)
});
< / script >
(document).ready(function () { function myfunction(e) { alert('Hello Test' + e); } @Html.Raw(ViewBag.js.Script) }); </script>





然而这打破了MVC模式,你的控制器不应该生成js,你应该做这样的事情





However this breaks the MVC pattern, your controller shouldn't be generating js, you should be doing something like this

public ActionResult Index()
{
    ViewBag.ID = 123;
    return View();
}







<script>


这篇关于从MVC中的控制器调用Javascript函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆