添加数据库填充的下拉 Razor

Adding a database populated dropdown Razor

刚开始学习使用 WebMatrix 制作动态页面,并且 运行 进入了路障。 (使用 notepad++ 的静态 html 和一些 php 的悠久历史,但这对我来说是新的。)

简而言之:我试图让人们可以选择在数据库中创建一个新的团队条目,或者从数据库填充的 html 助手下拉列表中加入现有团队。在两天的大部分时间里,我一直在尝试各种方法来执行此操作,但在这里和 ASP.net 上进行了大量搜索后,我似乎无法弄清楚。我也希望弄清楚如何在同一页面上创建新团队或加入数据库中已有的现有团队,而无需将逗号插入数据库。

我已经删除了我在下拉菜单中的所有尝试,并且正在分享当前没有 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");
        }

    }
}

.....剪断....

<!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>

任何方向将不胜感激! 谢谢。

您将分两步得到结果。先生成列表,再显示。

在页面顶部的代码块中添加此代码

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

然后在您希望下拉列表填充列表的地方

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

编辑:

<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>