我正在尝试在我的表单中选择一个select元素列出供用户选择的名称列表,尽管我的先前知识似乎不再正确。
这是我现在正在看的内容的总结视图。
@{
var productName = Request["ProductName"];
var productSupplierID = Request["SupplierID"];
var productCategoryID = Request["CategoryID"];
var productQuantity = Request["QuantityPerUnit"];
var productUnitPrice = Request["UnitPrice"];
var db = Database.Open("ProfessionalXM_DB");
var supplierQuery = "SELECT SupplierID, CompanyName FROM Suppliers ORDER BY CompanyName";
var categoryQuery = "SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryName";
var productSupplierName = Request["CompanyName"];
var productCategoryName = Request["CategoryName"];
var supplies = db.Query(supplierQuery, productSupplierID);
var categories = db.Query(categoryQuery);
if (IsPost)
{
var productsQuery = "INSERT INTO Products (ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock,UnitsOnOrder, ReorderLevel, Discontinued) VALUES(@0, @1, @2, @3, @4, 0, 0, 0, 0)";
try
{
db.Execute(productsQuery, productName, productSupplierID, productCategoryID, productQuantity, productUnitPrice);
}
catch (Exception ex)
{
<p>Error! @ex.Message</p>
}
}
<div class="col-md-12">
<form method="post">
<label>Name</label>
<input type="text" name="productName" required value="@productName" class="form-control" />
<label>Supplier</label>
<select name="productSupplier" required>
<option>@*Problem lies here*@</option>
</select>
</form>
我可以在网上找到的任何解决方案都是javascript或其他不幸的,在这种情况下是不允许的。
非常感谢任何帮助!
答案 0 :(得分:0)
如果您想要一个符合当前代码风格的解决方案,那么您需要做的就是获取供应商列表并编写一个循环来创建选择列表。你可以尝试这样的事情:
//Assuming that all the suppliers are stored in a list called suppliersList
var suppliersList = //write a query to get all the suppliers (Name and Code)
//In you HTML form, loop over all the supplier records in the suppliers list:
<select name="productSupplier" required>
@foreach (var supplier in suppliersList) {
<option value='@supplier.Code'>@supplier.Name</option>
}
</select>
希望这有帮助!