开发者

Executing javascript function upon returning result in MVC controller action

I'm stuck with the following scenario: There is a list of items on a page. Each item has a href that redirects it to the needed controller and action, i.e. when the list is populated each item's href is set to /Controller/Action/Parameter. Consequently, this is not a AJAX post, it's just a redirect when clicking on an item in the list (so no onSuccess unfortunately).

The respective controller action then retrieves a response from the repository. Now I need to display the response results in a dialog that pops up over the current page.

I currently implemented a solution with javascript: function ShowDialog(data). This displays the dialog and binds the data.

I have been trying without success to find a way to return a JavaScript result in my controller action that will call ShowDialog. Is this the way to go? Can I call javascript functions like that, r开发者_如何学JAVAeturning JavaScript(someScript)?


You might have better luck making a jQuery AJAX request. Bind a click event to the link, like this:

$(a.myLink).click(function(){
   $.ajax($(this).attr('href'), {
      success: function(data){
         showDialog(data);
      },
      'datatype': 'html'
   })
});

This requires your link to have a class myLink, but you can select the link(s) in any way you wish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜