how would I return a list of string in a json result in C# asp.net MVC?
I have this controller
public JsonResult AutoCompletePart(string id)
{
AutoCompleteService srv = new AutoCompleteService();
string[] parts = srv.AutoCompleteItemMaster(id);
//how do i return parts as JSON?
}
Thanks
These square brackets are used to declare JSON array [ ], JSON as they can store multiple values and value types, these values must be separated by ',' comma. Arrays in JSON are very much similar to arrays in JavaScript. So here empRights is an array of strings enclosed inside square brackets with quotations.
What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format). In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data .
If you need to send JSON in response to a GET, you'll need to explicitly allow the behavior by using JsonRequestBehavior. AllowGet as the second parameter to the Json method. However, there is a chance a malicious user can gain access to the JSON payload through a process known as JSON Hijacking.
Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.
Like this:
return Json(parts, JsonRequestBehavior.AllowGet);
This will return a simple Javascript string array.
If you want to return specific format, please provide more detail.
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