views:

368

answers:

2

I have two jQuery autocomplete textboxes on a mvc web page. One that returns a list of questions and another that returns a list of tags.

The questions textbox works perfectly but the tags text box only sends a null string to its controller.

The jQuery javascript is an exact match apart from the Url.Action, the non working one is displayed below:

<script type="text/javascript">
    $(document).ready(function() {
        $('#searchTag').autocomplete('<%= Url.Action("AutoComplete", "Tags") %>', {
            dataType: 'json',
            parse: function(data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i].Name, result: data[i].Name };
                }
                return rows;
            },
            formatItem: function(row) {
                return row.Name;
            },
            delay: 40,
            autofill: true,
            selectFirst: false,
            highlight: false,
            multiple: true,
            multipleSeparator: ";"
        });
    });
</script>

The Tags Controller is called and returns the Json data correctly as I have hard coded a 'b' string parameter to the LookUpTag method to make sure, but the string t parameter for AutoComplete is always null.

    public ActionResult AutoComplete(string t)
    {
        IQueryable<Tag> searchResults = tagRepository.LookUpTag("b");

        var data = (from searchResult in searchResults
                    select new { Id = searchResult.ID, Name = searchResult.Name }).ToList();
        return Json(data);
    }

Is there any logical reason for this?

+1  A: 
$('#searchTag')

Searching for an ID only returns a single element. Try using a classname.

Corey Hart
He's only showing the script for the one that isn't working, so I think its meant to be targeting one thing by id (and presumably there's another identical script targeting another field also by id).
Alconja
Ok, not sure how to do this. Changed the code to be: $('.searchForTag').autocomplete('<%= Url.Action("AutoComplete", "Tags") %>', <input id="searchTag" class="searchForTag" name="searchTag" type="text" value="Search for tag here" />
Nicholas Murray
On both of your inputs, you only need a classname, not an id. So:<input class="searchForTag" name="searchTag" type="text" value="Search for tag here" /><input class="searchForTag" name="searchTag" type="text" value="Search for tag here" /> AND --- $('.searchForTag').autocomplete('<%= Url.Action("AutoComplete", "Tags") %>', {...})
Corey Hart
@Corey - I think you've missed the point of what he's trying to do. He's got two text boxes, both of which need a *different* autocomplete URL. What you're suggesting would result in the same autocomplete results being populated in both text boxes.
Alconja
Thanks for your help. It was the t parameter as above.
Nicholas Murray
+1  A: 

Only thing I can see is that your parameter in your action is named t. If you're using this autocomplete plugin, then it looks like the search value will be sent through as a query string parameter named q (i.e. it'll request a url such as .../Tags/AutoComplete?q=b). The MVC will then try to match that query string parameter to a parameter on your action also called q.

So it might be as simple a fix as changing your action signature to:

public ActionResult AutoComplete(string q)
Alconja
Absolutely correct as the questions controller contains public ActionResult AutoComplete(string q) { IQueryable<Question> searchResults = questionRepository.LookUpQuestion(q); var data = (from searchResult in searchResults select new { Id = searchResult.QuestionID, Name = searchResult.QuestionTitle }).ToList(); return Json(data); }
Nicholas Murray