简体   繁体   中英

Return ActionResult to a Dialog. ASP.NET MVC

Given a method..

public ActionResult Method()
{
 // program logic
   if(condition)
   {
    // external library
    // external library returns an ActionResult
   }

 return View(viewname);
}

I cannot control the return type or method of the external library. I want to catch its results and handle that in a dialog on the page - but I cannot figure out how to get back to the page to execute the jQuery that would be responsible for it. Any ideas?

You can call Method() by just routing your jQuery .ajax() request to it. Since it's just returning straight up html, make sure you set your response type to expect that, and then your jQuery callback handler will have to deal with the resulting html. For example,

$("#myButton").click({
   $.ajax({
            // Basic ajax request properties
            url: /* route to call Method() */,
            data: {}
            dataType: "html",
            type: "GET",
            success: function(objResponse){
                   alert(objResponse);    // alerts the html of the result
                   /* Deal with response here, put the html in a dialog */
            }
      })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM