我正在使用 jqtree 通过类别过滤产品。它在我第一次单击类别时起作用。选择类别后,我将单击搜索按钮以显示部分视图中的项目数据。然后当我将选择另一个类别搜索另一组数据时,点击事件不再起作用。
我使用的jQuery代码:
function SearchProducts() {
var dataToSave = ko.toJSON({
productname: $('#txtProductName').val(),
categoryId: $('#lblCategoryID').text()
});
var url = '@Url.Action("AddEditProduct", "Inventory")';
$.ajax(url, {
data: dataToSave,
type: "post",
contentType: "application/json",
success: function (result) {
$("#addeditproduct").html(result);
}
});
};
$(function () {
var url = '@Url.Action("GetCategoryTreeView","Inventory")'
$('#SelectedCategoryID').text();
$('#tree1').tree({
dataUrl: url,
autoOpen: 1,
selectable: true
});
});
$('#tree1').bind(
'tree.click',
function (event) {
var node = event.node;
SelectedCategory = '';
lblParentID = '';
lblParentName = '';
$('#SelectedCategoryID').text('');
lblParentID = node.ID;
lblParentName = node.name;
SelectedCategory = 'Category Selected: ' + node.name + ' (Category ID: ' + lblParentID + ')';
$('#SelectedCategoryID').text(SelectedCategory);
$('#lblCategory').text(node.name + ' (' + lblParentID + ')');
lblCatID = lblParentID;
$('#lblCategoryID').text(lblParentID)
}
);
用于搜索产品的控制器代码
[HttpPost]
public ActionResult AddEditProduct(VMAddEditProduct viewModel)
{
var user = db.Users.Where(u => u.Username == this.User.Identity.Name).Single();
dynamic items = null;
if (viewModel.CategoryId != 0)
{
if (viewModel.ProductName != null && viewModel.ProductName != "")
{
items = (from product in db.tblFMSItems
join category in db.FMSCategory on product.CategoryID equals category.ID
where product.CenterID == user.CenterId
&& product.CategoryID == viewModel.CategoryId
&& product.ProductName == viewModel.ProductName
select new
{
CategoryId = product.CategoryID,
ProductId = product.ProductId,
ProductName = product.ProductName,
CategoryName = category.ProductType,
SKU = product.SKU,
Desc = product.Desc
})
.OrderBy(product => product.ProductName)
.ToList();
}
else
{
items = (from product in db.tblFMSItems
join category in db.FMSCategory on product.CategoryID equals category.ID
where product.CenterID == user.CenterId
&& product.CategoryID == viewModel.CategoryId
select new
{
CategoryId = product.CategoryID,
ProductId = product.ProductId,
ProductName = product.ProductName,
CategoryName = category.ProductType,
SKU = product.SKU,
Desc = product.Desc
})
.OrderBy(product => product.ProductName)
.ToList();
}
}
else if (viewModel.ProductName != null && viewModel.ProductName != "")
{
items = (from product in db.tblFMSItems
join category in db.FMSCategory on product.CategoryID equals category.ID
where product.CenterID == user.CenterId
&& product.ProductName == viewModel.ProductName
select new
{
CategoryId = product.CategoryID,
ProductId = product.ProductId,
ProductName = product.ProductName,
CategoryName = category.ProductType,
SKU = product.SKU,
Desc = product.Desc
})
.OrderBy(product => product.ProductName)
.ToList();
}
else if ((viewModel.ProductName == null) && viewModel.CategoryId == 0)
{
items = (from product in db.tblFMSItems
join category in db.FMSCategory on product.CategoryID equals category.ID
where product.CenterID == user.CenterId
select new
{
CategoryId = product.CategoryID,
ProductId = product.ProductId,
ProductName = product.ProductName,
CategoryName = category.ProductType,
SKU = product.SKU,
Desc = product.Desc
})
.OrderBy(product => product.ProductName)
.ToList();
}
var results = new List<VMAddEditProduct>();
foreach (var fmsitem in items)
{
var itemDesc = "";
if (fmsitem.Desc != null && fmsitem.Desc.Contains("<br/>"))
{
itemDesc = fmsitem.Desc.Replace("<br/>", "\n");
}
else
{
itemDesc = fmsitem.Desc;
}
var result = new VMAddEditProduct();
result.CategoryId = fmsitem.CategoryId;
result.CategoryName = fmsitem.CategoryName;
result.ProductId = fmsitem.ProductId;
result.SKU = fmsitem.SKU;
result.ProductName = fmsitem.ProductName;
result.Desc = itemDesc;
results.Add(result);
}
return View("~/Views/Report/AddEditProducts.cshtml", results);
}