Click here to Skip to main content
15,038,237 members
Please Sign up or sign in to vote.
4.20/5 (3 votes)
See more:
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) {

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);

1 solution

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900