【发布时间】:2018-12-17 06:58:27
【问题描述】:
asp.net core 的新手。我有一个数据库(让我们在 database123 中调用),其中包含表 Student。 Student 有 4 列,StudentId、StudentNameName、StudentGender 和 CreatedUtc。
通过 razor 页面向数据库添加数据没有问题。但我希望将 CreatedUtc 值插入数据库,而不是通过视图,而是通过控制器(它是创建记录的日期,应该来自 DateTime.Now.ToString 或类似的东西。
我无法确定在何处以及如何将其编码到 Create.cshtml.cs 控制器中。我认为它需要输入到公共异步任务 OnPostAsync() 中。
目前我有这个
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
MessageVar += $" Server time { DateTime.Now.ToString() }"; // Get Date and Time
_context.Student.Add(Student);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
在上面的 CreateModel 区域,我有这个:-
public string MessageVar { get; private set; } = "Local Server Time: ";
任何帮助将不胜感激。我知道这可能真的很简单,但就是找不到它。
提前致谢,
【问题讨论】:
标签: asp.net-core