我正在尝试允许提交博客的正文并使用html输出。但未提供错误,但为PageBody
,PublishDateUTC
,CreateDateUTC
或ModifyDateUTC
提交的任何内容均未保存到数据库中。我可以更改哪些内容以允许视图创建,读取,更新和删除此列记录?
Article.cs
(型号)
[AllowHtml]
public string PageBody { get; set; }
在ArticleController.cs
(控制器)中找到
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PostId,Title,PublishDate,CreateDate,ModifyDate,Author,PageBody,Feature,Published,Excerpt,PublishDateUTC,CreateDateUTC,ModifyDateUTC,CanonicalUrl,UrlSlug,Category,Tag")] ArticleContent articles)
{
if (ModelState.IsValid)
{
db.Articles.Add(articles);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(articles);
}
//Omitted
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PostId,Title,PublishDate,CreateDate,ModifyDate,Author,PageBody,Feature,Published,Excerpt,PublishDateUTC,CreateDateUTC,ModifyDateUTC,CanonicalUrl,UrlSlug,Category,Tag")] ArticleContent articles)
{
if (ModelState.IsValid)
{
db.Entry(articles).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(articles);
}
找到Edit.cshtml
(查看)
<div class="form-group">
<div class="col-xs-12">
@Html.LabelFor(model => model.PageBody, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PageBody, new { htmlAttributes = new { @class = "form-control", @id = "PageBody"} })
@Html.ValidationMessageFor(model => model.PageBody, "", new { @class = "text-danger" })
</div>
</div>
using System;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace JosephMCasey.Areas.Article.Models
{
public class ArticleContent
{
[Key]
public int PostId { get; set; }
[Required]
[StringLength(256, MinimumLength = 1, ErrorMessage = "Title cannot be longer than 256 characters.")]
public string Title { get; set;}
[Display(Name = "Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDate { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDate { get; set; }
[Display(Name = "Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDate { get; set; }
public string Author { get; set; }
[AllowHtml]
public string PageBody { get; set; }
public string Feature { get; set; }
public bool Published { get; set; }
public string Excerpt { get; set;}
[Display(Name = "UTC of Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDateUTC { get; set; }
[Display(Name = "UTC of Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDateUTC { get; set; }
[Display(Name = "GMT of Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDateUTC { get; set; }
public string CanonicalUrl { get; set; }
public string UrlSlug { get; set; }
public string Category { get; set; }
public string Tag { get; set; }
}
public class ArticleDBContext : DbContext
{
public DbSet<ArticleContent> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using JosephMCasey.Areas.Article.Models;
namespace JosephMCasey.Areas.Article.Controllers
{
public class ArticlesController : Controller
{
private ArticleDBContext db = new ArticleDBContext();
// GET: Article/Articles
public ActionResult Index()
{
return View(db.Articles.ToList());
}
// GET: Article/Articles/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ArticleContent articles = db.Articles.Find(id);
if (articles == null)
{
return HttpNotFound();
}
return View(articles);
}
// GET: Article/Articles/Create
public ActionResult Create()
{
return View();
}
// POST: Article/Articles/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PostId,Title,PublishDate,CreateDate,ModifyDate,Author,PageBody,Feature,Published,Excerpt,PublishDateUTC,CreateDateUTC,ModifyDateUTC,CanonicalUrl,UrlSlug,Category,Tag")] ArticleContent articles)
{
if (ModelState.IsValid)
{
db.Articles.Add(articles);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(articles);
}
// GET: Article/Articles/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ArticleContent articles = db.Articles.Find(id);
if (articles == null)
{
return HttpNotFound();
}
return View(articles);
}
// POST: Article/Articles/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PostId,Title,PublishDate,CreateDate,ModifyDate,Author,PageBody,Feature,Published,Excerpt,PublishDateUTC,CreateDateUTC,ModifyDateUTC,CanonicalUrl,UrlSlug,Category,Tag")] ArticleContent articles)
{
if (ModelState.IsValid)
{
db.Entry(articles).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(articles);
}
// GET: Article/Articles/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ArticleContent articles = db.Articles.Find(id);
if (articles == null)
{
return HttpNotFound();
}
return View(articles);
}
// POST: Article/Articles/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
ArticleContent articles = db.Articles.Find(id);
db.Articles.Remove(articles);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
@model JosephMCasey.Areas.Article.Models.ArticleContent
@{
ViewBag.Title = "Edit";
}
<section class="bg-ocean light row text-center shelve">
<div class="col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h2>Edit Articles</h2>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PostId)
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.PublishDate, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PublishDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PublishDate, "", new { @class = "text-danger" })
</div>
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.CreateDate, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.CreateDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreateDate, "", new { @class = "text-danger" })
</div>
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.ModifyDate, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.ModifyDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModifyDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
@Html.LabelFor(model => model.PageBody, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PageBody, new { htmlAttributes = new { @class = "form-control", @id = "PageBody"} })
@Html.ValidationMessageFor(model => model.PageBody, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Feature, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Feature, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Feature, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-6 col-md-3">
<div class="checkbox">
@Html.LabelFor(model => model.Published, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Published)
@Html.ValidationMessageFor(model => model.Published, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Excerpt, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Excerpt, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Excerpt, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.PublishDateUTC, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PublishDateUTC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PublishDateUTC, "", new { @class = "text-danger" })
</div>
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.CreateDateUTC, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.CreateDateUTC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreateDateUTC, "", new { @class = "text-danger" })
</div>
<div class="col-xs-12 col-md-4 border border-light">
@Html.LabelFor(model => model.ModifyDateUTC, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.ModifyDateUTC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModifyDateUTC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.CanonicalUrl, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.CanonicalUrl, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CanonicalUrl, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.UrlSlug, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.UrlSlug, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UrlSlug, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12 border border-light">
@Html.LabelFor(model => model.Tag, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Tag, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Tag, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-8 col-md-4 col-xs-12">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
</section>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我认为以下内容会解决它