Based on the code of Photo/View.cshtml page in Photo Gallery Template in Webmatrix, can someone help me to change it in order to use it for every page as comment section. I want to create a comment section for everypage but not using photoId (var photoId
= UrlData[0].AsInt();).
I want to place the code in every page and create a relation with user who posted and the page, based on page URL or something else (don't know exactly).
@{
var photoId = UrlData[0].AsInt();
var db = Database.Open("PhotoGallery");
var photo = db.QuerySingle("SELECT * FROM Photos WHERE Id = @0", photoId);
if (IsPost) {
var newComment = Request["newComment"].Trim();
if (newComment.IsEmpty()) {
Response.Redirect(Href("comment", photoId));
}
db.Execute("INSERT INTO Comments (PhotoId, CommentDate, UserId, CommentText) VALUES (@0, @1, @2, @3)",
photoId, DateTime.Now, WebSecurity.CurrentUserId, newComment);
Response.Redirect(Href("comment",photoId));
return;
}
// Found a match - so show its info
var comments = db.Query(@"SELECT * FROM Comments INNER JOIN UserProfiles
ON Comments.UserId = UserProfiles.UserId WHERE PhotoId = @0 ORDER BY Comments.CommentDate", photo.Id);
}
<div class="main">
@switch ((int)comments.Count) {
case 0:
<h2>Nobody has commented on this photo</h2>
break;
case 1:
<h2>This photo has generated one comment.</h2>
break;
default:
<h2>This photo has generated @comments.Count comments.</h2>
break;
}
<ol class="comments">
@foreach (var comment in comments) {
<li>
<h3 class="comment-header">
@comment.DisplayName commented at @comment.CommentDate:
</h3>
<p class="comment-body">@comment.CommentText</p>
</li>
}
</ol>
<form method="post" action="">
<fieldset id="addComment">
<legend>Post new comment</legend>
<ol>
<li>
<label for="newComment">Comment</label>
<textarea id="newComment" name="newComment" title="Your comment" rows="6" cols="70"></textarea>
</li>
</ol>
@if (WebSecurity.IsAuthenticated) { <p class="form-actions">
<input type="submit" title="Add comment" value="Add comment" />
</p>}else {<p>
Only registered user may leave comments. Please <a href="@Href("~/Account/Login")">login</a>
or <a href="@Href("~/Account/Register")">create an account</a> to leave a comment.
</p>
}
</fieldset>
</form>
</div>
Hi dow7, I think it will depend on what comments you want to show on each page.
It almost sounds like you want to use the page URL instead of the photoId? If this is the case, when you insert the comment into the database you can use something like this: Request.Url.AbsoluteUri
Then in your database add a field to the Comments table for "PageUrl" and use it to hold the Url.
However, I question doing this since it could make things less flexible.
You need some kind of identifier in the Comments table to tell you which comments to show, so it all depends on what comments you want to show on each page.
It sounds like you want the comments to be linked to the page, not the photo. In this case instead of storing photoid in the Comment record you would store the page identifier. For example "ab" or "ed". In that way, when you insert a comment it would
look more like this:
Isn't it possible without UrlData[0]? In the case of photoId there's only one page with the code block above. In my case i dont have the same structure of pages. Just want to put the code on each simple page and display the corresponding comments.
Yes, you do not need UrlData if you know exactly which group of comments you want. In that case you still need something like pageid in your Comments table so that you can filter it by which page you want to display it on.
In that case when you insert it would look like this:
dow7
Member
732 Points
449 Posts
A comment section for everypage without using photoId
Feb 14, 2012 03:26 PM|LINK
Based on the code of Photo/View.cshtml page in Photo Gallery Template in Webmatrix, can someone help me to change it in order to use it for every page as comment section. I want to create a comment section for everypage but not using photoId (var photoId = UrlData[0].AsInt();).
I want to place the code in every page and create a relation with user who posted and the page, based on page URL or something else (don't know exactly).
@{ var photoId = UrlData[0].AsInt(); var db = Database.Open("PhotoGallery"); var photo = db.QuerySingle("SELECT * FROM Photos WHERE Id = @0", photoId); if (IsPost) { var newComment = Request["newComment"].Trim(); if (newComment.IsEmpty()) { Response.Redirect(Href("comment", photoId)); } db.Execute("INSERT INTO Comments (PhotoId, CommentDate, UserId, CommentText) VALUES (@0, @1, @2, @3)", photoId, DateTime.Now, WebSecurity.CurrentUserId, newComment); Response.Redirect(Href("comment",photoId)); return; } // Found a match - so show its info var comments = db.Query(@"SELECT * FROM Comments INNER JOIN UserProfiles ON Comments.UserId = UserProfiles.UserId WHERE PhotoId = @0 ORDER BY Comments.CommentDate", photo.Id); } <div class="main"> @switch ((int)comments.Count) { case 0: <h2>Nobody has commented on this photo</h2> break; case 1: <h2>This photo has generated one comment.</h2> break; default: <h2>This photo has generated @comments.Count comments.</h2> break; } <ol class="comments"> @foreach (var comment in comments) { <li> <h3 class="comment-header"> @comment.DisplayName commented at @comment.CommentDate: </h3> <p class="comment-body">@comment.CommentText</p> </li> } </ol> <form method="post" action=""> <fieldset id="addComment"> <legend>Post new comment</legend> <ol> <li> <label for="newComment">Comment</label> <textarea id="newComment" name="newComment" title="Your comment" rows="6" cols="70"></textarea> </li> </ol> @if (WebSecurity.IsAuthenticated) { <p class="form-actions"> <input type="submit" title="Add comment" value="Add comment" /> </p>}else {<p> Only registered user may leave comments. Please <a href="@Href("~/Account/Login")">login</a> or <a href="@Href("~/Account/Register")">create an account</a> to leave a comment. </p> } </fieldset> </form> </div>beetledev
Member
750 Points
173 Posts
Re: A comment section for everypage without using photoId
Feb 14, 2012 03:50 PM|LINK
Hi dow7, I think it will depend on what comments you want to show on each page.
It almost sounds like you want to use the page URL instead of the photoId? If this is the case, when you insert the comment into the database you can use something like this: Request.Url.AbsoluteUri
Then in your database add a field to the Comments table for "PageUrl" and use it to hold the Url.
However, I question doing this since it could make things less flexible.
You need some kind of identifier in the Comments table to tell you which comments to show, so it all depends on what comments you want to show on each page.
dow7
Member
732 Points
449 Posts
Re: A comment section for everypage without using photoId
Feb 14, 2012 04:31 PM|LINK
Well : each page = own comment.
For instance : PageUrl = test.com/ab display ab_comments, test.com/ed display ed_comments.
beetledev
Member
750 Points
173 Posts
Re: A comment section for everypage without using photoId
Feb 14, 2012 05:44 PM|LINK
It sounds like you want the comments to be linked to the page, not the photo. In this case instead of storing photoid in the Comment record you would store the page identifier. For example "ab" or "ed". In that way, when you insert a comment it would look more like this:
db.Execute("INSERT INTO Comments (PageId, CommentDate, UserId, CommentText) VALUES (@0, @1, @2, @3)",
pageId, DateTime.Now, WebSecurity.CurrentUserId, newComment);
pageId would be 'ab' or 'ed', you would still need to set this somewhere before inserting the comment.
When you query the comments you can do something like:
Select CommentText from Comments where pageid = 'ab'
You would still need to use UrlData[0] if your url looks like: test.com/ab
Or, if ab and ed are actual page names (i.e. ab.cshtml and ed.cshtml) then you could still parse the URL to get the "ab" and "ed" portion.
dow7
Member
732 Points
449 Posts
Re: A comment section for everypage without using photoId
Feb 14, 2012 06:30 PM|LINK
beetledev
Member
750 Points
173 Posts
Re: A comment section for everypage without using photoId
Feb 14, 2012 07:04 PM|LINK
Yes, you do not need UrlData if you know exactly which group of comments you want. In that case you still need something like pageid in your Comments table so that you can filter it by which page you want to display it on.
In that case when you insert it would look like this:
db.Execute("INSERT INTO Comments (PageId, CommentDate, UserId, CommentText) VALUES (@0, @1, @2, @3)",
'ab', DateTime.Now, WebSecurity.CurrentUserId, newComment);
'ab' would have to be changed to something else if you are inserting for another page.
Normally I would not suggest hardcoding, but there is nothing stopping us from doing so.
dow7
Member
732 Points
449 Posts
Re: A comment section for everypage without using photoId
Feb 15, 2012 11:51 AM|LINK
Please can u give me just an example how to change the code in a way tah it can be implemented as part of sections in a layout page?
Or similar to the Disqus Helper using a helper, maybe if necessary.
dow7
Member
732 Points
449 Posts
Re: A comment section for everypage without using photoId
Feb 18, 2012 10:38 PM|LINK
Someone an idea, a little help pls?
dow7
Member
732 Points
449 Posts
Re: A comment section for everypage without using photoId
Mar 02, 2012 12:38 PM|LINK
Thanks on the answer of beetledev
i found a solution using an ID for each section of the page.