Can ASP.NET MVC return javascript response like Ruby on Rails?

I am immersed in ASP.NET MVC and I am coming from the Ruby on Rails background. I am trying to understand how ASP MVC handles AJAX functionality and, after reading some of the tutorials on the ASP website, it looks like they implement AJAX functionality in a completely different way. One of the ways RoR handles AJAX functionality is to return javascript code embedded in Ruby that runs as soon as it is received by the browser. This makes the AJAX implementation very simple and quite fun. Can ASP.NET MVC return javascript response?

+3
source share
3 answers

only user return javascript (script)

You will need to execute java script manually in the view

To be more specific, you can return the action type of the JavaScriptResult controller

+4
source

What you are saying is called javascript generators in the RoR world, and there is no equivalent in the ASP.NET MVC world. Here 's a blog post that illustrates the basics of implementing RJS-like RJS for ASP.NET MVC (the blog uses prototypejs, but can be easily adapted to work with jquery).


Here's another approach using jquery:

public ActionResult Foo()
{
    return Json(new { prop1 = "value1", prop2 = "value2" });
}

and consume:

$.getJSON('/home/foo', function(result) {
    // TODO: use javascript and work with the result here, 
    // the same way you would work in a RJS like template
    // but using plain javascript
    if (result.prop1 === 'value1') {
        alert(result.prop2);
    }
});
+3
source

JsonResult, ActionResult. , AJAX - .

+1

Source: https://habr.com/ru/post/1770574/


All Articles