In my AJAX call, I want to return a string value back to the calling page.
Should I use ActionResult
or just return a string?
You can use the Content method with the Content-Type text/html to return the HTML directly, without the need of Html. Raw . You can pass whatever Content-Type you want, such text/xml .
ContentResult return type is used for returning Content i.e. String, XML string, etc. from Controller to View in ASP.Net MVC Razor. Controller. The Controller consists of two Action methods. Action method for handling GET operation.
This process determines which view file is used based on the view name. The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called.
You can just use the ContentResult
to return a plain string:
public ActionResult Temp() { return Content("Hi there!"); }
ContentResult
by default returns a text/plain
as its contentType. This is overloadable so you can also do:
return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With