Having successfully integrated an autocomplete function to one of my projects using mikesdotnetting article
HERE, (nice facelift on the site btw Mike), I am now trying to implement the same but using a Directory.GetFiles command to autocomplete a filename lookup.
I realise this isn’t confined to Webmatrix & Web Pages but as that is the environment I am working in I thought I’d see if anyone had successfully achieved this?
Current code is below:
Main Page:
<div id="searchInput" class="searchInput"> <br /> <div id="searchdiv">Reg. Search:  <input type="text" id="search" name="search" class="search" /></div> <br /> </div>
<script> $(function () { $('.search').autocomplete({ source: '/lookups/lookup', minLength: 4 }); }); </script>
lookups/lookup:
@{ var files = (@"\serverinsurance$InformationBLANK FORMS - HEADER SHEETSSWOCLOSED CLAIMS"); foreach(var file in files){ reg = file; } var term = Request["term"] + "%"; var result = Directory.GetFiles(files,term, SearchOption.AllDirectories); var data = result.Select(p => new{label = p.???}); Json.Write(data, Response.Output); }
As you can see above, I have placed 3 ?’s where I am a bit stumped.
Using a DB Query, this would be the field to pull out of the DB but in this situation I am struggling to figure out what to replace it with?
Any help or points in the right direction would be great.
Cheers
NOTE:
Apologies for the misleading title!!
I had also tried via a Select > Option method but changed my approach after starting this thread!
NOTE:
I am closing this thread (marking as answer) as the title I have given it is complete tosh!
Do you just want the filenames returned? If so, your code will be this:
var data = result.Select(p => new { label = Path.GetFileName(p)});
You also need to change the "term" variable. The wildcard for file searches is an asterisk, not the percent sign:
var term = Request["term"] + "*";
Not sure what the foreach loop preceding that is all about, either.
As simple as that!
Working a treat thanks Mike.
The foreach was a throwback to a previous attempt.
Many thanks