I am building a comment system in asp.net MVC. Nothing fancy, but still a little bit above my skill level. I am using a partialview to display the comment form on the page, and need to have logged in users be able to comment and have the partial view reload what they have submitted.
My current code is as follows:
View:
@using Microsoft.AspNet.Identity
@model DCH.Web.Models.CollaborativeProjectDetailsViewModel
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>
tinymce.init({ selector: '#commentBox',
plugins: [
["advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker"],
["searchreplace wordcount visualblocks visualchars code insertdatetime media nonbreaking"],
["save table contextmenu directionality emoticons template paste"]
],
statusbar: false,
menubar: false
});
</script>
@{
var loggedInUser = User.Identity.GetUserName();
}
<!--Comments-->
<div>
<h3>Comments</h3>
<p> You are logged in as: <span class="commentUser">@loggedInUser</span>
@Html.ActionLink("(Log Out)", "LogOut", "Account")</p>
</div>
@using (Ajax.BeginForm("CommentForm", null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace }, new { id = "CollaborativeCommentForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
@Html.ValidationMessageFor(m => m.Content)
@Html.TextAreaFor(m => m.Content, new {style = "max-width: 1000px; width = 1000px", @class = "form-control", rows = "10", id = "commentBox"})
</div>
<div class="pull-right comment-submit-top-pad">
<input type="button" value="Cancel" class="btn btn-danger" />
<input type="button" value="Submit" class="btn btn-primary" />
</div>
<!--End Comments-->
}
Model:
[Required]
public int Id { get; set; }
public string PostedBy { get; set; }
public DateTime PostedOn { get; set; }
[Required(ErrorMessage = "Please fill in a comment below")]
[AllowHtml]
public string Content { get; set; }
Controller:
[HttpPost]
public ActionResult CommentForm(CollaborativeProjectDetailsViewModel model)
{
if (ModelState.IsValid)
{
Session["CollaborativeComments"] = model;
}
return PartialView("CollaborativeComments");
}
Now I went through the tutorials on asp.net and still am stuck on what to do. I know that I have to be able to db.Comments.Add but am unsure how to generate such code. Thank you in advance for any help.