Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoComplete control error: url.indexOf is not a function
    text
    copied!<p>I'm developing a WebSite in ASP.NET MVC3.</p> <p>Actually, I'm trying to implement an AutoComplete Control... In fact, when a user types more than two letters, I want to show an user list corresponding of these two letters.</p> <p>Two weeks ago, I've already success to do that, here's the result:</p> <p><img src="https://i.stack.imgur.com/hld8i.png" alt="enter image description here"> </p> <p><strong>Here's the code:</strong></p> <p><em>ReservationCalendar.cs</em></p> <pre><code>//AutoComplete control that return the name and first name of the partner corresponding to "term" public ActionResult Name(string term) { int id = 0; var user = User.Identity.Name; int inter = int.Parse(user); var managers = from d in db.Managers select d; managers = managers.Where(s =&gt; s.AffiliationNumber == inter); foreach (var manager in managers) { id = manager.TennisClubID; } var customers = (from c in db.Customers where (c.Name.Contains(term)) &amp;&amp; c.TennisClub.ID == id select c.Name + ";" + c.FirstName).Distinct().Take(10); return Json(customers, JsonRequestBehavior.AllowGet); } </code></pre> <p><strong><em>Create.cshtml</em></strong></p> <pre><code>@model TennisOnline.Models.Reservation @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt; &lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"&gt; &lt;/script&gt; &lt;link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" /&gt; &lt;script src="@Url.Content("~/Scripts/EditorHookup.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;link href="@Url.Content("~/Content/jquery.autocomplete.css")" rel="stylesheet" type="text/css"/&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $("#Partner").autocomplete({ source: '@Url.Action("Name", "ReservationCalendar")' }, { minChars: 2 }); }); &lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Reservation&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Date) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DisplayFor(model =&gt; model.Date) @Html.ValidationMessageFor(model =&gt; model.Date) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.StartTime) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DisplayFor(model =&gt; model.StartTime) @Html.ValidationMessageFor(model =&gt; model.StartTime) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Double) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Double) @Html.ValidationMessageFor(model =&gt; model.Double) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Customer.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DisplayFor(model =&gt; model.Customer.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Partner) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Partner) @Html.ValidationMessageFor(model =&gt; model.Partner) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.Label("Numéro du court") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DisplayFor(model =&gt; model.TennisCourt.Number) @Html.ValidationMessageFor(model =&gt; model.TennisCourt.Number) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Créer" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Retour à l'index", "Index", new { id = Model.TennisClub.ID }) &lt;/div&gt; </code></pre> <p><strong>JQuery.AutoComplete.js</strong></p> <pre><code>(function($) { "use strict"; /** * jQuery autocomplete plugin * @param {object|string} options * @returns (object} jQuery object */ $.fn.autocomplete = function(options) { var url; if (arguments.length &gt; 1) { url = options; options = arguments[1]; options.url = url; } else if (typeof options === 'string') { url = options; options = { url: url }; } var opts = $.extend({ }, $.fn.autocomplete.defaults, options); return this.each(function() { var $this = $(this); $this.data('autocompleter', new $.Autocompleter( $this, $.meta ? $.extend({ }, opts, $this.data()) : opts )); }); }; /** * Store default options * @type {object} */ $.fn.autocomplete.defaults = { inputClass: 'acInput', loadingClass: 'acLoading', resultsClass: 'acResults', selectClass: 'acSelect', queryParamName: 'q', limitParamName: 'limit', extraParams: { }, remoteDataType: false, lineSeparator: '\n', cellSeparator: '|', minChars: 2, maxItemsToShow: 10, delay: 400, useCache: true, maxCacheLength: 10, matchSubset: true, matchCase: false, matchInside: true, mustMatch: false, selectFirst: false, selectOnly: false, showResult: null, preventDefaultReturn: true, preventDefaultTab: false, autoFill: false, filterResults: true, sortResults: true, sortFunction: null, onItemSelect: null, onNoMatch: null, onFinish: null, matchStringConverter: null, beforeUseConverter: null, autoWidth: 'min-width', useDelimiter: false, delimiterChar: ',', delimiterKeyCode: 188, processData: null }; /** * Sanitize result * @param {Object} result * @returns {Object} object with members value (String) and data (Object) * @private */ var sanitizeResult = function(result) { var value, data; var type = typeof result; if (type === 'string') { value = result; data = { }; } else if ($.isArray(result)) { value = result[0]; data = result.slice(1); } else if (type === 'object') { value = result.value; data = result.data; } value = String(value); if (typeof data !== 'object') { data = { }; } return { value: value, data: data }; }; /** * Sanitize integer * @param {mixed} value * @param {Object} options * @returns {Number} integer * @private */ var sanitizeInteger = function(value, stdValue, options) { var num = parseInt(value, 10); options = options || { }; if (isNaN(num) || (options.min &amp;&amp; num &lt; options.min)) { num = stdValue; } return num; }; /** * Create partial url for a name/value pair */ var makeUrlParam = function(name, value) { return [name, encodeURIComponent(value)].join('='); }; /** * Build an url * @param {string} url Base url * @param {object} [params] Dictionary of parameters */ var makeUrl = function(url, params) { var urlAppend = []; $.each(params, function(index, value) { urlAppend.push(makeUrlParam(index, value)); }); if (urlAppend.length) { url += url.indexOf('?') === -1 ? '?' : '&amp;'; // ERROR HERE !!! url += urlAppend.join('&amp;'); } return url; }; .... })(jQuery); </code></pre> <p>Bizarrely, When I'm trying to use almost the "same" code in my "ReservationCalendar.cs", I get this JS error in Firebug: url.indexOf is not a function... <strong>in my js.file so</strong></p> <p>And he never reaches my Controller's method "Name"...</p> <p>Can you help me to find what I'm doing wrong please ? Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload