我在更新联结表上的复选框值时遇到问题。我有三个带有复选框的下拉列表(CategoryID,AgeID和GenderID),我可以选择每个下拉列表的多个表单。如果我选中或取消选中一个并运行我的更新没有任何反应。
例如,如果我取消选中CategoryID,它应该清除联结表上CategoryID为“2”的整行。
以下是我正在使用的代码:
var ProductID = UrlData[0];
var CategoryID = Request["Category"];
var GenderID = Request["Gender"];
var AgeID = Request["Age"];
var db = Database.Open("Data");
var sqlProduct = "SELECT * FROM Products WHERE ProductID = @0";
var product = db.QuerySingle(sqlProduct, ProductID);
var sqlProductDetail = "SELECT * FROM ProductDetail WHERE ProductID = @0";
var productDetail = db.QuerySingle(sqlProductDetail, ProductID);
var brandID = db.QueryValue("SELECT BrandID FROM Junction WHERE ProductID = @0", ProductID);
var categories = db.Query("SELECT CategoryID, CategoryName FROM Categories");
var categoriesChecked = db.Query("SELECT CategoryID FROM Junction WHERE ProductID = @0", ProductID).Select(c => (int)c.CategoryID);
var age = db.Query("SELECT AgeID, AgeName FROM Age");
var ageChecked = db.Query("SELECT AgeID FROM Junction WHERE ProductID = @0", ProductID).Select(a => (int)a.AgeID);
var gender = db.Query("SELECT GenderID, GenderName FROM Gender");
var genderChecked = db.Query("SELECT GenderID FROM Junction WHERE ProductID = @0", ProductID).Select(g => (int)g.GenderID);
var Category = CategoryID.Split(',');
var Gender = GenderID.Split(',');
var Age = AgeID.Split(',');
foreach (string crow in Category)
{
foreach (string arow in Age)
{
foreach (string grow in Gender)
{
//get the IDs
CategoryID = crow.Trim();
//get the IDs
AgeID = arow.Trim();
//get the IDs
GenderID = grow.Trim();
// Update
var sqlUpdateCategories = "UPDATE Junction SET CategoryID = @0, AgeID = @1, GenderID = @2, BrandID = @3 WHERE ProductID = @4 AND CategoryID = @0 AND AgeID = @1 AND GenderID = @2 AND BrandID = @3";
db.Execute(sqlUpdateCategories, CategoryID, AgeID, GenderID, brandID, ProductID);
}
}
}
这是我的HTML:
<ul class="dropdown-menu" role="menu">
@foreach (var item in categories.Select(i => (int)i.CategoryID))
{
<li>
<div class="checkbox">
<label>
<input type="checkbox" name="Category" value="@item" checked="@(categoriesChecked.Contains(item))" /> @categories.First(c => c.CategoryID == item).CategoryName
</label>
</div>
</li>
}
</ul>
<ul class="dropdown-menu" role="menu">
@foreach (var item in age.Select(i => (int)i.AgeID))
{
<li>
<div class="checkbox">
<label>
<input type="checkbox" name="Age" value="@item" checked="@(ageChecked.Contains(item))" /> @age.First(a => a.AgeID == item).AgeName
</label>
</div>
</li>
}
</ul>
<ul class="dropdown-menu" role="menu">
@foreach (var item in gender.Select(i => (int)i.GenderID))
{
<li>
<div class="checkbox">
<label>
<input type="checkbox" name="Gender" value="@item" checked="@(genderChecked.Contains(item))" /> @gender.First(g => g.GenderID == item).GenderName
</label>
</div>
</li>
}
</ul>