1

我在详细信息页面中创建了一个评论部分,但是当有人在一个特殊页面中评论时,可以在详细信息页面的每一页中看到它。我不知道问题出在哪里。

特别是我陷入了代码中的 foreach 循环。

这是代码:

    @{
dynamic item = null;
if(!Request["id"].IsEmpty() && Request["id"].IsInt()) {
var db = Database.Open("Classifieds");
var sql = "SELECT * FROM Items, Categories WHERE Id = @0";
item = db.QuerySingle(sql, Request["id"]);
Page.Title = item.Title + "-" + item.Id;
}


if(IsPost) {
if(Request["name"].IsEmpty()) {
ModelState.AddError("name", "Please provide a name");
}
if(Request["email"].IsEmpty()) {
ModelState.AddError("email", "Please enter your E-mail");
}
if(!Request["email"].IsEmpty() && !Functions.IsValidEmail(Request["email"])) {
ModelState.AddError("email","Please provide a valid email");
}
if(Request["comment"].IsEmpty()) {
ModelState.AddError("comment", "Please fill the comment form");
}
if(!ModelState.IsValid) {
ModelState.AddFormError("Please fix the errors below before submitting your comment");
}

else {
**var db = Database.Open("Classifieds");
var msql = @"INSERT INTO Comments (Name, Email, Comment, DatePosted) VALUES (@0, @1,   @2, GetDate())";
var parameters = new[]{Request["name"],Request["email"],Request["comment"]};
db.Execute(msql, parameters) ;
Response.Redirect("~/Details.cshtml?id=" + item.Id);**
}
}
**var dbs = Database.Open("Classifieds");
var tsql = @"SELECT Id, Name, Email, Comment, DatePosted FROM Comments";
var data = dbs.Query(tsql).OrderByDescending(c => c.Id);**

}

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function resetForm() {
document.getElementById("name0").value = "";
document.getElementById("email0").value = "";
document.getElementById("comment0").value = "";
}
</script>
</head>
<body>
@if(item != null) {
<h2>Details</h2>
<h3>@item.Title</h3>
<p>@item.Description</p>
<p>Price: @item.Price.ToString("c")</p>
<p>Condition: @item.Condition</p>
<p>Posted on @item.DatePosted.ToString("D") in the @item.Category category</p>
}




<br><br><hr>

<div>@Html.ValidationSummary(true)</div>

<form id="post-advert" method="post" action="@Href("~/Details.cshtml?id=" + item.Id)">
<div data-href="@Href("~/Details.cshtml?id=" + item.Id)">
**@if(item.Id == Request["id"].AsInt()) {
foreach(var dt in data) {
<div id="namecomment">Posted by @dt.Name at @dt.DatePosted</div><br>
<div id="commentsection">@dt.Comment</div><hr>
Cache.Remove(dt.ToString());
}
}**

</div><br><br><br>

<div><label for="name">Name* : </label></div>

<div><input type="text" name="name" id="name0" value="@Request["name"]">
@Html.ValidationMessage("name")
</div>

<div><label for="email">E-mail* : </label></div>
<div>

<input type="text" name="email" id="email0" value="@Request["email"]">
@Html.ValidationMessage("email")
</div>

<div><label for="comment">Comment* : </label></div>
<div>
<textarea name="comment" cols="40" rows="10" id="comment0" >@Request["comment"]</textarea>
@Html.ValidationMessage("comment")
</div><br>
<div><input type="submit" value="Post Comment"></div>
</form>
</body>
</html>
4

1 回答 1

1

发生这种情况是因为您没有任何东西可以唯一标识评论的用途。存储/检索您提供的评论时

  • 姓名
  • 电子邮件
  • 评论
  • 发布日期

没有指定您要显示的详细信息页面。您应该将您的 Id 添加到表和后续查询中。

于 2012-07-03T16:39:20.780 回答