Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return a list of string as JSONResult

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

like image 382
twal Avatar asked Jul 12 '10 18:07

twal


People also ask

How do you give an array of strings in JSON?

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 C#?

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 .

What is JsonRequestBehavior AllowGet?

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.

Does JSON have Array?

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.


1 Answers

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.

like image 91
SLaks Avatar answered Oct 07 '22 18:10

SLaks