locked
Razor Web Page select option with Jquery Lookup RRS feed

  • Question

  • User927770920 posted

    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: &nbsp<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 = (@"\\server\insurance$\Information\BLANK FORMS - HEADER SHEETS\SWO\CLOSED 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

    Monday, November 3, 2014 8:27 AM

Answers

  • User927770920 posted

    NOTE:

    I am closing this thread (marking as answer) as the title I have given it is complete tosh!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 3, 2014 8:44 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 3, 2014 8:58 AM

All replies

  • User927770920 posted

    NOTE:

    Apologies for the misleading title!!

    I had also tried via a Select > Option method but changed my approach after starting this thread!

    Monday, November 3, 2014 8:33 AM
  • User927770920 posted

    NOTE:

    I am closing this thread (marking as answer) as the title I have given it is complete tosh!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 3, 2014 8:44 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 3, 2014 8:58 AM
  • User927770920 posted

    As simple as that!

    Working a treat thanks Mike.

    The foreach was a throwback to a previous attempt.

    Many thanks

    Monday, November 3, 2014 9:03 AM