有一个管理面板,管理员可以在其中添加,编辑或删除新闻。每个新闻都由"标题","正文"和"创建"组成。它为网站用户显示。
但是,日期("创建"(存在问题。例如,新闻是在 1 月 25 日创建的,但管理员在 1 月 27 日对其进行了编辑。在此之后,它显示为 1 月 27 日而不是 1 月 25 日 - 这是不正确的,因为它是在两天前创建的。
我知道这是因为当管理员更新新闻和 DateTime.Now 函数刷新创建值时。如何记住"创建"中的"发布"创建时间?
后.cs
namespace Blog.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; } = "";
public string Body { get; set; } = "";
public DateTime Created { get; set; } = DateTime.Now;
}
}
面板控制器.cs
[HttpGet]
public IActionResult Edit(int? id)
{
if (id == null)
{
return View(new PostViewModel());
}
else
{
var post = _repo.GetPost((int)id);
return View(new PostViewModel
{
Id = post.Id,
Title = post.Title,
Body = post.Body
});
}
}
[HttpPost]
public async Task<IActionResult> Edit(PostViewModel vm)
{
var post = new Post
{
Id = vm.Id,
Title = vm.Title,
Body = vm.Body
};
if (post.Id > 0)
_repo.UpdatePost(post);
else
_repo.AddPost(post);
if (await _repo.SaveChangesAsync())
return RedirectToAction("Index");
else
return View(post);
}
您可以尝试在更新期间设置IsModified
属性。这里是例子
if (post.Id > 0)
{
_repo.UpdatePost(post);
_repo.Entry(post).Property(x => x.Created).IsModified = false;
}
如果要记住创建和更新日期,则必须添加另一个属性。
不要将"创建时间"设置为"日期时间.Now",因为这可能会覆盖实际保存的日期。
public class Post
{
public int Id { get; set; }
public string Title { get; set; } = "";
public string Body { get; set; } = "";
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}
此外,在相应地创建或更新资源时设置这些值。
[HttpPost]
public async Task<IActionResult> Edit(PostViewModel vm)
{
var post = new Post
{
Id = vm.Id,
Title = vm.Title,
Body = vm.Body
};
if (post.Id > 0)
{
post.Updated = DateTime.Now;
_repo.UpdatePost(post);
}
else
{
post.Created = DateTime.Now;
post.Updated = DateTime.Now; // If you want Created == Updated on creation
_repo.AddPost(post);
}
if (await _repo.SaveChangesAsync())
return RedirectToAction("Index");
else
return View(post);
}
有两种简单的方法可以将此存档到当前模型中:
1. 您可以通过Created
属性扩展PostViewModel
,并在 GET 请求中相应地设置它。之后,您可以在 POST 请求中再次访问它,并将其分配给新创建的Post
对象。您必须在EditView
中为此属性添加一个隐藏字段,否则它将不会在您的 POST 请求中发回。
[HttpGet]
public IActionResult Edit(int? id)
{
if (id == null)
{
return View(new PostViewModel());
}
else
{
var post = _repo.GetPost((int)id);
return View(new PostViewModel
{
Id = post.Id,
Title = post.Title,
Body = post.Body,
Created = post.Created // Remember the created date
});
}
}
[HttpPost]
public async Task<IActionResult> Edit(PostViewModel vm)
{
var post = new Post
{
Id = vm.Id,
Title = vm.Title,
Body = vm.Body,
Created = vm.Created // Re-Assign the created date
};
if (post.Id > 0)
_repo.UpdatePost(post);
else
_repo.AddPost(post);
if (await _repo.SaveChangesAsync())
return RedirectToAction("Index");
else
return View(post);
}
阿拉伯数字。 另一种可能性是在Edit
POST 请求中获取现有的Post
对象并更新现有对象的属性:
[HttpPost]
public async Task<IActionResult> Edit(PostViewModel vm)
{
var post = _repo.GetPost(vm.Id);
if(post == null)
post = new Post();
post.Title = vm.Title;
post.Body = vm.Body;
if (post.Id > 0)
_repo.UpdatePost(post);
else
_repo.AddPost(post);
if (await _repo.SaveChangesAsync())
return RedirectToAction("Index");
else
return View(post);
}