2

I am hoping someone can help me. I am sure this is quite simple, but I have spent many hours searching for the answer and nothing seems to be working. I have a CSHTML file in Microsoft WebMatrix with Razor/C# code and HTML markup as well as a basic SQL table. I would like to put a dropdown list which displays the results of a SQL query. Below is my code.

@{

var db = Database.Open("QualityMonitoring") ;
var listAgent = "SELECT Agent FROM Data";

}

How do I create a dropdown list to display the results in my markup?

Any help would be greatly appreciated.

1
  • What is the name of your dropdownlist Commented Sep 1, 2013 at 14:42

2 Answers 2

4

You have to create a SelectedListItem list and bind a dropdownlist to it. Extend the code to:

@{
    var db = Database.Open("QualityMonitoring") ;
    var listAgent = "SELECT Agent FROM Data";

    List<SelectListItem> agentdropdownlistdata = new List<SelectListItem>();
    bool isSelected = false;
    foreach(var item in db.Query(listAgent)){   
        agentdropdownlistdata.Add(new SelectListItem
        {
            Text = item.AgentName,
            Value = item.AgentID.ToString(), 
            Selected = isSelected
        });
    }
}

And in the markup use html helper to bind the data:

 @Html.DropDownList("Agents", agentdropdownlistdata)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks afzalulh, I have applied your code and markup, but I am getting the following error. Compiler Error Message: CS1526: A new expression requires (), [], or {} after type Source Error: Line 7: foreach(var item in db.Query(listAgent)){ Line 8: isSelected = false; Line 9: agentdropdownlistdata.Add(new SelectList Item Line 10: { Line 11: Text = item.AgentName,
Sorry for the typo! It should be SelectListItem, not SelectList Item. I have edited my answer.
Thanks. This worked, although I had to comment out Value = item.ID.ToString(), as this produced an error on rendering. How would I make it so no default value is selected? It can say "Please Choose..." for example?
You can add a new SelectListItem as selected: agentdropdownlistdata.Add(new SelectListItem {Text = "Select one",Value = "0", Selected = true }); outside the foreach loop.
2

Try this and tell me if the problem gets solved:

DropDownlist.DataSource = listAgent;

4 Comments

Thanks for replying so quickly. listAgent is the variable which contains the SQL statement. Do I put your suggestion in the code or the markup? I am not sure how to create the dropdown list in the markup. Since it is Razor, I am thinking something which would require @ before it.
Well i am not quite familiar with razor, but the code above i showed you must be put in the code section not markup
@afzalulh, how can I give this dropdown list a name so I can pass its value to a variable?
In the design view select the control and change its name in properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.