MVC:如何调用Javascript函数? [英] MVC: How to call Javascript function in action?

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

问题描述

在我看来,我有一个名为 testalert 的函数,在我的控制器中我有一个名为索引的动作,我使用 javascriptmodel 可以解决我的问题,但我发现如果我的动作没有返回一个view(),例如:只返回Json(模型),javascriptmodel将无法正常工作。如何当我返回json时调用js函数?为什么javascriptmodel只能在返回视图中正常工作?

In my view I has a function called testalert,and in my controller I has a action called Index,I use javascriptmodel can solve my problem,but I find that if my action do not return a view(),for example:just return Json(model),the javascriptmodel will not work.How to call js function when I return json?Why do the javascriptmodel only be designed to work well in return view?

function testalert(para) {
     alert(para);
    }

public ActionResult Index()
    {
  //work well  and alert "abc"
 this.AddJavaScriptFunction("testalert", PageLoadEvent.Ready, null, "abc");
        return View();
    }
public ActionResult GetData()
    {
  var restult="data";
 // not work
 this.AddJavaScriptFunction("testalert", PageLoadEvent.Ready, null, "abc");
   return Json(restult);
    }

推荐答案

如何在JSON类型的返回中尝试执行JavaScript函数,它不是工作。



很简单,在第一个操作中返回是一个VIEW,然后在VIEW添加脚本以执行。在第二个操作中,返回是一个JSON,没有如何将JSON内的脚本放在VIEW上执行。
How you are trying execute a JavaScript function inside a return of type JSON, It's not work.

It's simple, in the first action the return is a VIEW, then the script is added at VIEW to be executed. In the second action the return is a JSON, there aren't how put the script inside of the JSON to be executed on VIEW load.


这篇关于MVC:如何调用Javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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