0

I am a newbie learning from an Autocomplete textbox example. What I don't understand is in the "AutoCompleteHelper" function in my controller, the input must be named "term". If I name it to something else, like "mystr" or "reqstr", then during debug, it will always say the input is "null". I didn't define the "term" anywhere else in my .js and .cshtml. I actually called it name="q" in the .cshtml file. What is making me use the name "term"?

Controller code:

    string[] txtlst = {"ActionScript", "AppleScript",
        "Asp", "BASIC", "C", "C++", "Clojure",
        "COBOL", "ColdFusion", "Erlang", "Fortran",
        "Groovy", "Haskell", "Java", "JavaScript",
        "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"};

public JsonResult AutoCompleteHelper(string term) {
            if (!Request.IsAjaxRequest()) return null;
            var lst = txtlst.Where(s => s.StartsWith(term)).ToList();
            var res = lst.Select(x => new { value = x, label = x });
            return Json(res, JsonRequestBehavior.AllowGet);
        }

.js code:

    $("input[data-autocomplete-source]").each(function () {
        $(this).autocomplete({ source: $(this).attr("data-autocomplete-source") });
    });
});

.cshtml code:

<p>Auto Complete Example</p>
<input type="text" name="q" data-autocomplete-source="@Url.Action("AutoCompleteHelper", "Home")" />

Am I overlooking/misunderstanding something? I can't see why I am fixed to use "term" as my param name.

2 Answers 2

1

The autocomplete makes an AJAX call to fetch the data, and uses the name term in the querystring, e.g. AutoCompleteHelper?term=Brainfuck.

The MVC framework automatically matches the query string and form content against the parameters in the action method, so the value from the querystring with the name term is used for the term parameter when the method is called.

Sign up to request clarification or add additional context in comments.

6 Comments

ohh okay! So, is there anywhere I can define my own specific param name? i thought my name="q" was doing that, if not, what is that doing? In general, how do I check what parameter(s) and their name(s) each different Helper is passing to the query string? Sorry about my newbie spam questions.
@Tom I think you set it in your controller with AutoCompleteHelper(string term) if you change it to AutoCompleteHelper(string q) i think it will change it to q
@Manatherin no, did u try that? when I run it, q is null. it can only be term.
@Tom: There might be a setting in the autocomplete call that defines the query string name. It doesn't use the name of the field, because then you couldn't use the same server method for different input fields.
@Manatherin: The client code can't see the name of the parameter in the action method, so changing that doesn't change what's used to send the request.
|
0

Why not do this in pure JS and give a way more fluent experience? It's mostly static anyway:

var txtlst = [  "ActionScript", "AppleScript",
                "Asp", "BASIC", "C", "C++", "Clojure",
                "COBOL", "ColdFusion", "Erlang", "Fortran",
                "Groovy", "Haskell", "Java", "JavaScript",
                "Lisp", "Perl", "PHP", "Python", "Ruby",
                "Scala", "Scheme"];

$("input[data-autocomplete-source]").each(function () {
    $(this).autocomplete({ source: txtlst });
});

2 Comments

because the filtered list is dynamically generated from server, we don't want to pass thousands of items to client script in a real situation.
@Tom: Oh, I see, that was a bad example. :D

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.