[RESOLVED]dynamically inserting data into a Dropdownbox with C# and Razor

This is all in my .cshtml file. My DB connections work fine, I am getting all the data I need. I want to use a @foreach command (which I have successfully used in other parts of my program ) to make the data from the SELECT statement the data in the DropdownBox.
I am not using the MVC model. I am using webpages and C#.

Is there a simple way to do this ? Below is the code I am starting with

Thanks

Todd

@{
    
    var db  = Database.Open("Aikido");
    var query = db.Query("SELECT * from UserProfile where active = ‘true’");
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
         <h1> Student Roster </h1>
        @{Layout="AdminLayout.cshtml";}

        @foreach (var Status in query)        {
        items.Add(new SelectListItem
         {
            Text= @Status.email,
            Value = @Status.id.ToString()
         }) ;
        
}
    </body>
</html>

Do a search for "cascading dropdowns" at
www.mikesdotnetting.com
.

Note how the dropdowns are constructed with the html helpers in code and then displayed in markup.

Hello,

<form action="" method="post">

...

    <div>
        <select name="catid">
            @foreach(var Status in query){
                <option value="@Status.CatID">@Status.CatName</option>
            }
        </select>

    </div>
    <div>
        <input type="submit" name="post" value="Post">
    </div> 

</form>

You need to change CatID and CatName with yours..

Mark as answer if it helped..

I tried to get to fancy! Embarassed  Should have known it would have been something simple !!

Thanks a ton !

Todd

Leave a Reply