使用 MVC Orchard 服务文件允许用户在"HoursWorkedToday"表中输入新记录,允许用户输入他们当天工作的小时数。
//HoursWorkedService
public class HoursWorkedService : IHoursWorkedService
{
private IRepository<HoursWorkedPerDay> _repository = null;
public HoursWorkedService(IRepository<HoursWorkedPerDay> Repository)
{
_repository = Repository;
}
public Models.HoursWorkedPerDay Create(HoursWorkedPerDay newRecordForHoursWorked)
{
_repository.Create(newRecordForHoursWorked);
return newRecordForHoursWorked;
}
//IHoursWorkedService
public interface IHoursWorkedService : IDependency
{
HoursWorkedPerDay Create(HoursWorkedPerDay newRecordForHoursWorked);
}
//controller
[HttpPost, ActionName("HoursWorkedPerDay")]
public ActionResult HoursWorkedPerDayPOST(int userId, int hours)
{
DateTime TodaysDate = DateTime.UtcNow;
HoursWorkedPerDay HoursPerDay = new HoursWorkedPerDay();
HoursPerDay.Date = TodaysDate;
HoursPerDay.WorkerRecord_Id = userId;
HoursPerDay.HoursWorked = hours;
_hoursWorked.Create(HoursPerDay);
_notifier.Add(NotifyType.Information, T("Thank you, Hours Added."));
return RedirectToAction("Index");
}
但是在帖子之后,我得到"网站无法显示"
日志显示:
不能将值 NULL 插入到列"Id"、表 'Orchard.Timekeeper.dbo.Timekeeper_HoursWorkedPerDay' 中;列不允许空值。插入失败
在我的迁移文件中,我将"id"设置为 .ContentPartRecord((,我假设在使用服务时会自动创建下一条记录
不要对不是内容部分记录的内容使用 ContentPartRecord。相反,请自行将 Id 列添加为标识,以便它自动递增。如果它旨在作为内容部件记录,则切勿在没有内容项的情况下使用它。特别是,切勿直接在部件记录上使用存储库,而要通过内容管理器。