Using Webmatrix, how do I save data not displayed in the select in a foreach loop?

时间:2016-05-17 11:15:36

标签: drop-down-menu webmatrix

I am desperate! I have a list of people with names and a unique ID. I need to display just the first and last name in a drop down list and then when the user selects the name they want from the list and submits, I would like to create a new record in a different table and add:

  • first name
  • last name
  • unique ID

I just don't know the syntax to do it. I have tried everything and this is the closest I get which works for one record in the list but multiple records in the list fails.

 <select class="form-control" name="">
     <div>@foreach(var Team in MyTeam){
            <option value="">@Team.FirstName @Team.LastName </option>
            <input type="hidden" name="FirstName" value=@Team.FirstName>
            <input type="hidden" name="LastName" value=@Team.LastName>    
            <input type="hidden" name="TeamID" value=@Team.UserID> 
            <input type="hidden" name="ReferrerName" value=@Team.UserID>  
                                       }     
     </div>

1 个答案:

答案 0 :(得分:0)

您不需要隐藏字段,它们将无法工作。

<select class="form-control" name="formUser">
        @foreach(var Team in MyTeam){
        <option value="">@Team.FirstName @Team.LastName </option>
        }     
</select>

然后,您需要添加帖子代码

if(IsPost){
   var userID = Request["formUser"];
   var sqlselect = "SELECT * FROM YourTable Where UserID = @0";
   var sqldata = db.QuerySingle(sqlselect, userID);
   var firstname = sqldata.FirstName;
   var lastname = sqldata.LastName;
   //And so on to get your variables then you can use them to insert
}