如何从asp.net-mvc控制器动作返回原始数组

时间:2011-02-19 12:58:14

标签: php jquery asp.net-mvc

我是trying to use this plugin多个鸟类(远程)示例),但后端示例是在php中,我的后端是asp.net-mvc。我试图将这个PHP代码翻译成asp.net-mvc。是否可以从asp.net-mvc控制器操作返回一个数组(而不是在Json或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";
    }
}

?>

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

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

并在您看来:

<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" />
相关问题