autocomplete extraparams is always nullhttp://forums.asp.net/t/1658781.aspx/1?autocomplete+extraparams+is+always+nullFri, 04 Nov 2011 09:29:10 -040016587814324871http://forums.asp.net/p/1658781/4324871.aspx/1?autocomplete+extraparams+is+always+nullautocomplete extraparams is always null <p>Hello,</p> <p>I am trying to implement autocomplete to a textbox using jquery. I am using MVC. I am trying to pass extra parameters but they are always null or jquery never add it to the query string.</p> <p>Any suggestion?</p> <p>View:</p> <p><pre class="prettyprint">$(document).ready(function () { $(&quot;input#tbxFilter&quot;).autocomplete({ source:'@Url.Action(&quot;Filter&quot;, &quot;Circuit&quot;)', cacheLength: 0, delay: 500, extraParam: { &quot;title&quot;: &quot;test&quot;} }); });</pre> </p> <p>@Html.TextBox("tbxFilter")</p> <p>Controller:</p> <p> <pre class="prettyprint"> public ActionResult Filter(string term, string title) { string valueToUse = System.Web.HttpContext.Current.Request.QueryString["title"]; return RedirectToAction("Index"); }</pre> </p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2011-03-02T02:28:55-05:004324955http://forums.asp.net/p/1658781/4324955.aspx/1?Re+autocomplete+extraparams+is+always+nullRe: autocomplete extraparams is always null <p>Hi,</p> <p>You can do the following-</p> <p><strong>View-</strong></p> <p><pre class="prettyprint">&lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt; &lt;link href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt; &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js&quot;&gt;&lt;/script&gt; &lt;h2&gt; AutocompletereturnsError&lt;/h2&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div class=&quot;demo&quot;&gt; Extra1 &lt;input type=&quot;text&quot; id=&quot;extra1&quot; /&gt;&lt;br /&gt; Extra2 &lt;input type=&quot;text&quot; id=&quot;extra2&quot; /&gt;&lt;br /&gt; &lt;div class=&quot;ui-widget&quot;&gt; &lt;input type=&quot;text&quot; name=&quot;name-list&quot; id=&quot;name-list&quot; /&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt; $(document).ready(function() { $(&quot;#name-list&quot;).autocomplete({ source: function(request, response) { $.ajax({ url: '&lt;%=Url.Action(&quot;FindBarcode&quot;, &quot;ASPForum&quot;)%&gt;', data: &quot;bc=&quot; &#43; request.term &#43; &quot;&amp;limit=5&quot; &#43; &quot;&amp;extra1=&quot; &#43; $(&quot;#extra1&quot;).val() &#43; &quot;&amp;extra2=&quot; &#43; $(&quot;#extra2&quot;).val(), type: &quot;GET&quot;, dataType: &quot;json&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function(data) { response($.map(data, function(item) { return { label: item.barcode, value: item.id } })) }, error: function(XMLHttpRequest, textStatus, errorThrown) { debugger; alert(textStatus); } }); } }); }); &lt;/script&gt; &lt;/form&gt; &lt;/asp:Content&gt;</pre> </p> <p><strong>Controller-</strong></p> <p> <pre class="prettyprint"> public ActionResult AutocompletereturnsError() { return View(); } public JsonResult FindBarcode(string bc, int limit, string extra1, string extra2) { var result = (new[] { new { id = 1, barcode = "abc" } }).ToList(); result.Add(new { id = 2, barcode = "abcb" }); result.Add(new { id = 3, barcode = "abcd" }); result.Add(new { id = 4, barcode = "abce" }); result.Add(new { id = 5, barcode = "abcr" }); result.Add(new { id = 6, barcode = "abcf" }); result.Add(new { id = 7, barcode = "abcg" }); result.Add(new { id = 8, barcode = "abch" }); result.Add(new { id = 9, barcode = "abcj" }); result.Add(new { id = 10, barcode = "abcff" }); result.Add(new { id = 11, barcode = "abcffd" }); result.Add(new { id = 12, barcode = "cfsdfsdf" }); result.Add(new { id = 13, barcode = "ckkk" }); result.Add(new { id = 14, barcode = "ckkkrr" }); result.Add(new { id = 15, barcode = "ckkkggg" }); result.Add(new { id = 16, barcode = "ckkkfffdsfd" }); result.Add(new { id = 17, barcode = "ckkkfdfd" }); return Json(result.FindAll(c =&gt; c.barcode.IndexOf(bc) == 0).Take(limit), JsonRequestBehavior.AllowGet); }</pre> </p> 2011-03-02T04:12:10-05:004325186http://forums.asp.net/p/1658781/4325186.aspx/1?Re+autocomplete+extraparams+is+always+nullRe: autocomplete extraparams is always null <p>Is there a way not using JSON? I am little bit confused with the code.</p> 2011-03-02T07:00:05-05:004325355http://forums.asp.net/p/1658781/4325355.aspx/1?Re+autocomplete+extraparams+is+always+nullRe: autocomplete extraparams is always null <p>Hi,</p> <p>When you are orking with MVC and ajax the best practice is to pass data is json result. Just pass your object to Json() method and return the result. What is confusing here?</p> 2011-03-02T08:42:47-05:004667675http://forums.asp.net/p/1658781/4667675.aspx/1?Re+autocomplete+extraparams+is+always+nullRe: autocomplete extraparams is always null <p>Instead of using extraParams try setting the param in the Url.Action() call</p> <p>eg</p> <pre class="prettyprint">$(document).ready(function () { $(&quot;input#tbxFilter&quot;).autocomplete({ source:'@Url.Action(&quot;Filter&quot;, &quot;Circuit&quot;, new { &quot;title&quot; = &quot;test&quot;})', cacheLength: 0, delay: 500 }); });</pre> 2011-11-04T09:29:10-04:00