leora
leora

Reputation: 196741

how to return raw array from asp.net-mvc controller action

i am trying to use this plugin (the multiple birds (remote) example), but the backend example is in php and my backend is asp.net-mvc. I am trying to translate this php code into asp.net-mvc. Is it possible to just return an array from an asp.net-mvc controller action (versus doing it in Json or XML)

<?php

$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(
"Great <em>Bittern</em>"=>"Botaurus stellaris",
"Little <em>Grebe</em>"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Common Chiffchaff"=>"Phylloscopus collybita",
"House Finch"=>"Carpodacus mexicanus",
"Green Heron"=>"Butorides virescens",
"Solitary Sandpiper"=>"Tringa solitaria",
"Heuglin's Gull"=>"Larus heuglini"
);

foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        echo "$key|$value\n";
    }
}

?>

Upvotes: 0

Views: 564

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

You could use the following:

public ActionResult Search(string q)
{
    // fetch those from the database
    var values = new[] { "value1", "value2", "value3" };

    // filter based on the search string the user entered
    var result = values.Where(x => x.Contains(q));

    // render them to the response
    return Content(string.Join("\n", result), "text/plain");
}

and in your view:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    $('#items').autocomplete('@Url.Action("search")');
});
</script>
<input type="text" id="items" name="items" />

Upvotes: 1

Related Questions