我有一个下拉列表,我已经填充了数据库中的项目。下拉列表显示项目名称,但选中的值是项目的ID而不是名称。
我想在另一个表上插入一条新记录,但我收到以下错误,我试图研究这个问题:
编译器错误消息:CS0103:当前上下文中不存在名称“customerID”
@{
Layout = "~/_Layout.cshtml";
Page.Title = "Add Server";
var db = Database.Open("BackupMonitoringAppV2") ;
var listcustomers = "SELECT CustomerName, CustomerID from Customers";
List<SelectListItem> customerdropdowndata = new List<SelectListItem>();
bool isSelected = false;
foreach(var item in db.Query(listcustomers)){
customerdropdowndata.Add(new SelectListItem
{
Text = item.CustomerName,
Value = item.CustomerID.ToString(),
Selected = isSelected
});
}
///Need to resolve the issue with request form adding to database.
var servername = "";
if(IsPost){
customerID = Request.Form["Customers"];
servername = Request.Form["ServerName"];
var db2 = Database.Open("BackupMonitoringAppV2");
var insertCommand = "INSERT INTO Servers (CustomersID, Servername,) Values(@0, @1)";
db.Execute(insertCommand, customerID, servername);
Response.Redirect("~/Dashboard/Servers");
}
<form role="form" method="post">
<div class="form-group">
@Html.DropDownList("Customers", customerdropdowndata)
</div>
<div class="form-group">
<label for="CustomerName">Server Name</label>
<input type="text" class="form-control" name="customername" value="@Request.Form["ServerName"]" />
</div>
<button type="submit" class="btn btn-info" name="buttonSubmit">Add Server</button>
</form>