locked
Using data from a table to create a User Modified Menu (WebMatrix) RRS feed

  • Question

  • User784974013 posted

    Flexable Menu Code:

    I used the below code example that obtains the menu item text from a user table named Departments, where DepartmentName is a field.  The user is permitted to create the menu item names (Department Name) through a add/delete department name routines (not shown).  The menu item selected by the user utilizes and passes the department name from Menu.cshtml to the StandardPage.cshtml, and modifies the query to display the data in the StandardPage.cshtml.  This permits utilization of one page of code common for any department name selected by the user and displays the appropriate data from the Contacts table.

    Menu Example:

    Departments

    Administration

    Bingo

    Cage

    Finance

    Food and Beverage

    Gift Shop

    Hotel

    Human Relations

    Information Systems

    Keno

    Maintenance

    Code for Menu.cshtml:

    @{
        Layout = "~/_SiteLayout.cshtml";
        Page.Title = "Directory";
    
    }
     
    @section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@Page.Title.</h1>
                <h2>Department Menu</h2>
            </hgroup>
        </div>
    </section>
    }
    @{
        <h2>Departments</h2>
        <br />
        var db = Database.Open("People");
        var query = "SELECT * FROM Departments ORDER BY DepartmentName";
        var dept = db.Query(query);
        
        try{
            foreach(var row in dept){
            string Dept=@row.DepartmentName;
            <a HREF="~/StandardPage?id=@Dept">@row.DepartmentName</a><br />
            }
        }
        catch(Exception ex)
            {
                
             }
        }
        <a data-role="button" id="button-rt" href="~/" title="cancel" style="float: right">Cancel</a>
    The below code (StandardPage.cshtml) is called from the menu item selected to display the data for the selected department name in a webgrid. Contacts in this example is the table with employee names, phone #, etc. String result is passed to this code by the Menu selection code (above example).
    @{
        Layout = "~/_SiteLayout.cshtml";
        string result = Request["id"];
        Page.Title = result;
        
        var recordcount = 0;
        var db = Database.Open("People");
        var query = db.Query("select * from Contacts WHERE Department ='" + result + "' ORDER BY Name");
        foreach(var contact in query){
            recordcount = recordcount + 1;
        }
        var grid = new WebGrid (query);    
    }
     
    @section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@Page.Title.</h1>
                    @if(recordcount >0){
                        <h3>Click on a heading title to sort the Record Data by heading topic.</h3>
                    }
            </hgroup>
        </div>
    </section>
    }
     
     @{
        if(recordcount == 0) {
            <h2>No records found.</h2>
        }
        if(recordcount == 1){
            @grid.GetHtml( 
            caption: recordcount + " MATCHING RECORD FOUND",
            tableStyle: "grid", 
            headerStyle: "head", 
            alternatingRowStyle: "alt", 
            columns: grid.Columns( 
            grid.Column(format: @<a href="~/Services/Edit?id=@item.ContactID">Edit</a>), 
            grid.Column("Name"), 
            grid.Column("Title"), 
            grid.Column("Phone"), 
            grid.Column("Cell"),
            grid.Column(format: @<a href="~/Services/Delete?id=@item.ContactID">Delete</a>) 
            )
        )
        }
        if(recordcount > 1){
            @grid.GetHtml( 
            caption: recordcount + " MATCHING RECORDS FOUND",
            tableStyle: "grid", 
            headerStyle: "head", 
            alternatingRowStyle: "alt", 
            columns: grid.Columns( 
            grid.Column(format: @<a href="~/Services/Edit?id=@item.ContactID">Edit</a>), 
            grid.Column("Name"), 
            grid.Column("Title"), 
            grid.Column("Phone"), 
            grid.Column("Cell"),
            grid.Column(format: @<a href="~/Services/Delete?id=@item.ContactID">Delete</a>) 
                )
            )
        }
    }
     
    <br />
    <div id="Addnew">
        <a data-role="button" id="button-lf" href="~/Services/Add">Add New</a>
        <a data-role="button" id="button-rt" href="~/Services/Menu" title="cancel" style="float: right">Cancel</a>
    </div>
    Friday, December 7, 2012 11:39 PM

All replies

  • User619859613 posted

    Thank you for your valuable experience to share.

    Tuesday, December 11, 2012 2:11 AM