使用 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(),我假设它在使用服务时自动创建了下一条记录