简体   繁体   中英

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";
    }
}

?>

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM