Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Last active July 22, 2017 00:58
Show Gist options
  • Save GER-NaN/ff8c7738f99c05b9d99d35c9d4e0434f to your computer and use it in GitHub Desktop.
Save GER-NaN/ff8c7738f99c05b9d99d35c9d4e0434f to your computer and use it in GitHub Desktop.
MVCComment Poster
@model IEnumerable<WebsiteComments.Models.Comment>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Subject)
</th>
<th>
@Html.DisplayNameFor(model => model.Text)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Score)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@* *@
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.Text)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Score)
</td>
</tr>
}
</table>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebsiteComments.Models
{
public class Comment
{
public int CommentId { get; set; }
[StringLength(20)]
public string UserName { get; set; }
[StringLength(20)]
public string Subject { get; set; }
[StringLength(500)]
public string Text { get; set; }
public DateTime Date { get; set; }
public int Score { get; set; }
}
}
@model IEnumerable<WebsiteComments.Models.Comment>
@{
ViewBag.Title = "ViewAllComments";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<span class="comment-label">User</span>
@Html.TextBox("UserName", null, new { htmlAttributes = new { @class = "form-control" } })
<span class="comment-label"> Subject</span>
@Html.TextBox("Subject", null, new { htmlAttributes = new { @class = "form-control" } })
<div class="comment-multiline">
<div class="comment-multiline-label">
Comment
</div>
<textarea class="comment-multiline-textarea" type="text" name="Text" placeholder="Tip or Comment" rows="3"></textarea>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
<h2>Comments</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Subject)
</th>
<th>
@Html.DisplayNameFor(model => model.Text)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Score)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@* *@
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.Text)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Score)
</td>
</tr>
}
</table>
using System.Data.Entity;
namespace WebsiteComments.Models
{
public class CommentContext : DbContext
{
public CommentContext(): base("name=Comment")
{
}
public DbSet<Comment> Comments { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebsiteComments.Models;
namespace WebsiteComments.Controllers
{
public class CommentController : Controller
{
[HttpGet]
public ActionResult Comments()
{
var allComments = new List<Comment>();
using (var db = new CommentContext())
{
allComments.AddRange(db.Comments);
}
return View(allComments);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Comments(Comment newComment)
{
newComment.Date = DateTime.UtcNow;
newComment.Score = 1;
newComment.UserName = string.IsNullOrWhiteSpace(newComment.UserName) ? "Unknown visitor" : newComment.UserName;
newComment.Subject = string.IsNullOrWhiteSpace(newComment.Subject) ? "P0174 code help" : newComment.Subject;
//Save it and return to list
using (var db = new CommentContext())
{
db.Comments.Add(newComment);
db.SaveChanges();
}
return RedirectToAction("Comments");
}
}
}
body {
padding-top: 50px;
padding-bottom: 20px;
}
/* Set padding to keep content from hitting the edges */
.body-content {
padding-left: 15px;
padding-right: 15px;
}
/* Override the default bootstrap behavior where horizontal description lists
will truncate terms that are too long to fit in the left column
*/
.dl-horizontal dt {
white-space: normal;
}
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
max-width: 280px;
}
/**********************************************************
Add new Comment Box
*/
.comment-multiline {
border: grey solid 1px;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 6px;
padding-right: 6px;
margin: 3px;
}
.comment-multiline-active {
border: blue solid 1px;
}
.comment-multiline-idle{
border: grey solid 1px;
}
.comment-multiline-label {
clear: both;
}
.comment-multiline-textarea {
border-top: none;
border-left: none;
border-right: none;
border-bottom: grey solid 1px;
}
/**********************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment