Ad

How To Get A String From Controller And Use It In View?

I'm trying to get some string from my Controller and use it in the View. The ActionResult works like this:

public ActionResult GetSymbols()
{
    string result = "SVG-String";
    return Content(result);
}

This result is will be a svg-formatted string and should be shown in my svg-drawing, at the end. I tried to call this controller-Action, using JavaScript and I could reach the Controller but how could I use the String? I couldn't see any result, so what is the rigth way to get the returned string into a variable?

The last try was like this:

$(document).ready(function () {
            $.ajax({
                url: "/Symbols/GetSymbols/",
                method: "GET",
                async: false,
                data: "",
                dataType: "string",
                success: function (data) { alert(data); }
            });
    });
Ad

Answer

How about this one,

Change your Controller return type to Json

[HttpGet]
public JsonResult GetSymbols()
{
    string result = "SVG-String";
    return Json(result, JsonRequestBehavior.AllowGet);
}

And your javascript be like,

$(document).ready(function () {
    $.ajax({
        method: "GET",
        url: "/Symbols/GetSymbols/",
        async: false,
        dataType: "json",
        success: function (data) { 
            alert(data); 
        },
        error: function (response) {
            console.log(response);
        }
    });
});
Ad
source: stackoverflow.com
Ad