开发者

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

开发者 https://www.devze.com 2023-02-11 21:37 出处:网络
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.n

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

精彩评论

暂无评论...
验证码 换一张
取 消