0

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

?>

1 Answer 1

1

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" />
Sign up to request clarification or add additional context in comments.

4 Comments

i am trying to do this on the back of an ajax call.. how does the php work then??
@ooo, the result is directly written to the response.
Darin, which collection would you use if the requirement is to render the key/value pairs in the original order (whose sorting criteria is unknown)? A dictionary doesn't preserve the insertion order, right? And a SortedList wouldn't help either. Thought? TIA.
Serge - appTranslator, SortedDictionary<TKey, TValue>, or simply reorder the elements in the controller action using the desired order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.