简体   繁体   中英

Adding a database populated dropdown Razor

just started learning to make dynamic pages with WebMatrix and have run into a road block. (Long history of static html and some php using notepad++, but this is new to me.)

In short: I'm trying to give people the option of creating a new team entry in the database OR joining an existing team from a html helper dropdown list populated from the database. I've been trying various ways of doing this for the better part of two days and can't seem to figure it out after extensive searchers here and on ASP.net. I'm also hoping to figure out how to allow either the creation of a new team or joining an existing team already in the DB on the same page without getting commas inserted into the database.

I've removed all my attempts at a dropdown and am sharing what currently works without the html.dropdown:

@{
    Validation.RequireField("FName", "Your first name is required");
    Validation.RequireField("LName", "Your last name is required");
    Validation.RequireField("Team", "A team is required to play");
    Validation.RequireField("Pledge", "You must donate to play.");

    var db = Database.Open("BBBT");
    var FName = Request.Form["FName"];
    var LName = Request.Form["LName"];
    var Team = Request.Form["Team"];
    var Pledge = Request.Form["Pledge"];

    if (IsPost && Validation.IsValid()) {
        if(ModelState.IsValid) {
            var insertQuery = "INSERT INTO Players (FName, LName, Team, Pledge) " +
                "VALUES (@0, @1, @2, @3)";
            db.Execute(insertQuery, FName, LName, Team, Pledge);
            Response.Redirect("~/ListTeams");
        }

    }
}

....Snip....

<!DOCTYPE html>
<html>
<head>
 <title>Register</title>
</head>
<body>
 <h1>Register</h1>

 @Html.ValidationSummary("Errors with your submission:")

 <form method="post" action="">
   <fieldset>
     <legend>Join the fun!</legend>
     <div>
       <label>First Name:</label>
       <input name="FName" type="text" size="50" value="@FName" />
      </div>
      <div>
       <label>Last Name:</label>
       <input name="LName" type="text" size="50" value="@LName" />
     </div>
     <div>
       <label>Create a Team</label>
       <input name="Team" type="text" size="50" value="@Team" />
        </div>
       <div>
       <label>Or Joing an Existing Team</label>
<!-- This is where I would like to put the Dropdown list-->
        @Html.DropDownList
        </div>
     <div>
       <label>Pledge:</label>
       <input name="Pledge" type="text" size="50" value="@Pledge" />
     </div>
     <div>
       <label>&nbsp;</label>
       <input type="submit" value="Insert" class="submit" />
     </div>
   </fieldset>
 </form>
</body>
</html>

Any direction would be greatly appreciated! Thank you.

Your will be getting the result in two steps. First Generate the List, then display it.

In the code block at the top of the page add this code

List<string> Teams = new List<string>();
foreach(var row in db.Query("SELECT DISTINCT Team FROM Players"))
{
Teams.Add(row.Team);
}

and in the place you want the dropdown populate the list

<select name="Team">
@foreach(string Teamname in Teams)
{
<option value="@Teamname ">@Teamname </option>
}
</select>

EDIT:

<select name="Team" id="TeamDropdown" onchange="toggleteamcreation()">
    @foreach(string Teamname in Teams)
    {
        <option value="@Teamname ">@Teamname </option>
    }
    <option value"New">Create New Team</option>
</select>
<script>
function toggleteamcreation(){
    if(document.getElementById('TeamDropdown').value=="New"){
        document.getElementById('Create_New_Team').style.display="block";
    }
    else{
        document.getElementById('Create_New_Team').style.display="none";
    }
}
</script>
<div id="Create_New_Team" style="display:none">
   <label>Create a Team</label>
   <input name="Team" type="text" size="50" value="@Team" />
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM