我正在尝试使用来自数据库的数据平均填充两个不同的容器。将5个位置放在第一个容器中,将6个位置放在第二个容器中。每次运行此代码时,它都会将所有内容填充到左侧容器中。 任何想法我错过了什么?
shopDataContext dc = new shopDataContext();
var areas = (from s in dc.Areas select s);
String content = "";
foreach(var a in areas) {
if (a.id >= 1 && a.id <= 6) {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
leftContent.InnerHtml = leftContent.InnerHtml + content;
} else {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
rightContent.InnerHtml = rightContent.InnerHtml + content;
}
}
答案 0 :(得分:0)
我想你想在循环的每次迭代中重置添加的内容:
...
foreach(var a in areas) {
string content = "";
if (a.id >= 1 && a.id <= 6) {
...
}
}
....
否则会多次添加内容。