感谢提醒,所以我也添加了模型的内容。我有一个视图,其中有一个表单可以将模型发布到操作,现在它的工作原理如下:
<script src="@Url.Content("~/Scripts/ckeditor/ckeditor.js")">
</script>
<br />
<br />
@using (Html.BeginForm("BlogSubmit", "Blog", FormMethod.Post, new { @class = "container"}))
{
<br />
<br />
<br />
<p class="text-warning">Blog Title</p>
<div class="span6">
@Html.TextBoxFor(m => m.BlogTitles.Titles, new { @class = "input-xxlarge", @placeholder="Your blog title is here",@style="width: 500px"})
</div>
<br />
<br />
<br />
<p class="text-warning">Blog Content</p>
@Html.TextAreaFor(m => m.BlogContents.Contents, new { @id = "editor1" });
<br />
<button type="submit" class="btn btn-success">Submit Blog</button>
}
<script>
CKEDITOR.replace("editor1");
</script>
然后在下面的操作中,获取blogmodel
的模型包含博客的所有信息。
blogmodel
[HttpPost]
[ValidateInput(false)]
public ActionResult BlogSubmit(BlogPageModel blogData)
{
blogData.Blogs.LastModified = DateTime.Now;
DB.BlogContenttbl.Add(new BlogContent { Contents = blogData.BlogContents.Contents });
DB.BlogTitletbl.Add(new BlogTitle { Titles = blogData.BlogTitles.Titles });
DB.SaveChanges();
var blogTitleID = from t in DB.BlogTitletbl
where t.Titles == blogData.BlogTitles.Titles
select t.TitleID;
var blogContentID = from c in DB.BlogContenttbl
where c.Contents == blogData.BlogContents.Contents
select c.ContentID;
int blogTitleIDToInt = Convert.ToInt32(blogTitleID.First());
int blogContentIDToInt = Convert.ToInt32(blogContentID.First());
DB.Blogtbl.Add(new Blog { TitleID = blogTitleIDToInt, ContentID = blogContentIDToInt, LastModified = DateTime.Now });
DB.SaveChanges();
TempData["blog"] = blogData;
return RedirectToAction("BlogPost");
}
模型
这里我要澄清的是这个模型视图不是映射到表的实例,而只是我为视图专门创建的模型。
public class BlogPageModel
{
public Blog Blogs { get; set; }
public BlogAid BlogAids { get; set; }
public BlogAuthor BlogAuthors { get; set; }
public BlogComment BlogComments { get; set; }
public BlogContent BlogContents { get; set; }
public BlogNews BlogNews { get; set; }
public BlogTitle BlogTitles { get; set; }
}
但是,实际上我希望获得博客 lastmodified 时间并在 blogpost 视图中显示,映射到表格的博客真正模型如下:
public class Blog
{
public int BlogID { get; set; }
public DateTime LastModified { get; set; }
public System.Nullable<int> AidID { get; set; }
public virtual BlogAid BlogAid{ get; set; }
public System.Nullable<int> AuthorID { get; set; }
public virtual BlogAuthor BlogAuthor { get; set; }
public System.Nullable<int> CommentID { get; set; }
public virtual BlogComment BlogComment { get; set; }
public System.Nullable<int> TitleID { get; set; }
public virtual BlogTitle BlogTitle { get; set; }
public System.Nullable<int> ContentID { get; set; }
public virtual BlogContent BlogContent { get; set; }
public System.Nullable<int> NewsID { get; set; }
public virtual BlogNews BlogNews { get; set; }
}
我真的不知道为什么它不起作用。并且,我在这行代码中得到了以下错误:
blogData.Blogs.LastModified = DateTime.Now;
对象引用未设置为对象的实例。
答案 0 :(得分:-1)
尝试实例化日期时间,例如
[...]
var now = new DateTime();
now = DateTime.Now;
DB.Blogtbl.Add(new Blog { TitleID = blogTitleIDToInt, ContentID = blogContentIDToInt, LastModified = now });
[...]
答案 1 :(得分:-1)
new DateTime(DateTime.Now.Ticks)