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
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
Lyly
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.
nilphilus
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)
lang-cs