forked from hallatore/MvcTechdaysBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tore Lervik
committed
Sep 5, 2011
1 parent
c6aa9a2
commit fb5fa55
Showing
11 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,51 @@ | ||
using System.Web.Mvc; | ||
using System; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using MvcTechdaysBlog.Models; | ||
|
||
namespace MvcTechdaysBlog.Controllers | ||
{ | ||
public partial class HomeController : Controller | ||
{ | ||
{ | ||
private DataService db = new DataService(); | ||
|
||
public virtual ActionResult Index() | ||
{ | ||
var articles = db.Articles.OrderBy(a => a.Date).ToList(); | ||
return View(articles); | ||
} | ||
|
||
public virtual ActionResult Article(string id) | ||
{ | ||
var article = db.Articles.SingleOrDefault(a => a.Url == id); | ||
|
||
if (article != null) | ||
{ | ||
ViewBag.Comments = article.Comments.OrderByDescending(c => c.Date).ToList(); | ||
return View(article); | ||
} | ||
|
||
throw new HttpException(404, "NotFound"); | ||
} | ||
|
||
[HttpPost] | ||
public virtual ActionResult PostComment(Comment comment) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
comment.Date = DateTime.Now; | ||
db.Comments.Add(comment); | ||
db.SaveChanges(); | ||
} | ||
|
||
return PartialView("Comment", comment); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
db.Dispose(); | ||
base.Dispose(disposing); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
|
||
namespace MvcTechdaysBlog.Models | ||
{ | ||
public class Comment | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
[MaxLength(50)] | ||
public string Name { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.MultilineText)] | ||
[MaxLength(500)] | ||
[Remote("BadWords", "Validation")] | ||
public string Content { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Date)] | ||
public DateTime Date { get; set; } | ||
|
||
public int ArticleId { get; set; } | ||
|
||
public virtual Article Article { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@model MvcTechdaysBlog.Models.Article | ||
|
||
@{ | ||
ViewBag.Title = @Model.Title; | ||
} | ||
|
||
<div class="two-third"> | ||
<div class="article"> | ||
<h2>@Model.Title</h2> | ||
<p>@Html.Raw(Model.Content)</p> | ||
</div> | ||
</div> | ||
|
||
<div class="one-third last"> | ||
@Html.Partial("CommentForm", new MvcTechdaysBlog.Models.Comment { ArticleId = Model.Id }) | ||
@if (ViewBag.Comments != null) | ||
{ | ||
<div class="comments"> | ||
@foreach (MvcTechdaysBlog.Models.Comment comment in ViewBag.Comments) | ||
{ | ||
@Html.Partial("Comment", comment) | ||
} | ||
</div> | ||
} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@model List<MvcTechdaysBlog.Models.Article> | ||
|
||
@{ | ||
ViewBag.Title = "Index"; | ||
} | ||
|
||
<div class="one"> | ||
@foreach (var article in Model) | ||
{ | ||
<div class="article"> | ||
<a href="@Url.Action("Article", new { id = article.Url })"><h2>@article.Title</h2></a> | ||
<p>@article.Description</p> | ||
<a href="@Url.Action("Article", new { id = article.Url })" class="fancy-button" style="margin-top: 10px;">Les mer</a> | ||
</div> | ||
} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@model MvcTechdaysBlog.Models.Comment | ||
|
||
<div class="comment"> | ||
<div class="comment-name">@Model.Name</div> | ||
<div class="comment-date">@Model.Date.ToString("dd.MM.yyyy - HH:mm")</div> | ||
<div class="comment-content">@Model.Content</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
@model MvcTechdaysBlog.Models.Comment | ||
|
||
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script> | ||
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"> </script> | ||
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"> </script> | ||
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"> </script> | ||
<script type="text/javascript"> | ||
function onCommentPostSuccess(data) { | ||
$(".comments").prepend($(data)); | ||
cancelCommentForm(); | ||
} | ||
$(function() { | ||
$(".comment-form-button").click(function() { | ||
$(".comment-form").slideDown(function() { | ||
$(".comment-form #Name").focus(); | ||
}); | ||
$(".comment-form-button").slideUp(); | ||
}); | ||
$(".comment-form-cancel").click(cancelCommentForm); | ||
}); | ||
function cancelCommentForm() { | ||
$(".comment-form").slideUp(); | ||
$(".comment-form-button").slideDown(); | ||
$(".comment-form input[type='text'], .comment-form textarea").val(""); | ||
} | ||
</script> | ||
|
||
<div class="comment-form" style="display: none;"> | ||
@using (Ajax.BeginForm("PostComment", "Home", new AjaxOptions { OnSuccess = "onCommentPostSuccess" })) | ||
{ | ||
@Html.ValidationSummary(true) | ||
<fieldset> | ||
<div class="editor-label"> | ||
@Html.LabelFor(model => model.Name) | ||
</div> | ||
<div class="editor-field"> | ||
@Html.TextBoxFor(model => model.Name) | ||
@Html.ValidationMessageFor(model => model.Name) | ||
</div> | ||
|
||
<div class="editor-label"> | ||
@Html.LabelFor(model => model.Content) | ||
</div> | ||
<div class="editor-field"> | ||
@Html.TextAreaFor(model => model.Content) | ||
@Html.ValidationMessageFor(model => model.Content) | ||
</div> | ||
|
||
@Html.HiddenFor(model => model.ArticleId) | ||
<input type="submit" value="Post" /> <div class="fancy-button comment-form-cancel">Cancel</div> | ||
</fieldset> | ||
} | ||
</div> | ||
<div class="comment-form-button fancy-button">New comment</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters