0

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);
 }
tereško
58.5k26 gold badges100 silver badges151 bronze badges
asked Dec 19, 2013 at 15:38

1 Answer 1

1

I've done f.e AsyncHelper.Call(url, params) which return as a result promise, and on clientside wait for promise.done and to my stuff.

shorter version:

var AsyncAction = (function () {
return {
 //options: passed to $.ajax 
 Call: function (options, helperOptions) {
 return $.ajax(options)
 .done(function (result) {
 helperOptions.onSucceed(result.model);
 });
 }
};

})();

answered Dec 19, 2013 at 15:44

2 Comments

I'm afraid that I can't adapt to the situation.My situation is somebody call the action which may not from the view or may from another program,and once the action be called,the js function will be called.
I do not exactly know what do you mean @Lyly by 'another program' but if you call some action which return json data which should be processed or whatever this is best solution cause you are returning json, not view - and in this case adding sth by this.AddJavascript will not affect view (will not be reloaded)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.