[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



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

<!DOCTYPE html>

<html lang="en">
        <meta charset="utf-8" />
         <h1> Student Roster </h1>

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

Do a search for "cascading dropdowns" at

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


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


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

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


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 !


Leave a Reply