即使我选择了正确的日期值,我也无法弄清楚为什么我会收到此错误。我还希望当用户点击创建课程时,日期应该自动填写。
“SqlDateTime溢出。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间”
$
#CoursePart.cs
```
using System;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Records;
using Orchard.ContentManagement.Utilities;
namespace Orchard.Club.Models
{
public class CoursePart : ContentPart<CoursePartRecord>
{
public string Name
{
get { return Record.Name; }
set { Record.Name = value; }
}
public string Description
{
get { return Record.Description; }
set { Record.Description = value; }
}
public string Location
{
get { return Record.Location; }
set { Record.Location = value; }
}
public string Category
{
get { return Record.Category; }
set { Record.Category = value; }
}
[DataType(DataType.Date)]
public DateTime StartDate
{
get { return Record.StartDate; }
set { Record.StartDate = value; }
}
[DataType(DataType.Date)]
public DateTime EndDate
{
get { return Record.EndDate; }
set { Record.EndDate = value; }
}
public double Fees
{
get { return Record.Fees; }
set { Record.Fees = value; }
}
}
public class CoursePartRecord : ContentPartRecord
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Location { get; set; }
public virtual string Category { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual double Fees { get; set; }
}
}
```
#Course.cshtml
```
@model Orchard.Club.Models.CoursePart
@using Telerik.Web.Mvc.UI;
@{
Script.Include("~/Scripts/jquery-1.6.2.min.js");
Script.Include("~/Scripts/modernizr-2.0.6-development-only.js");
}
<fieldset>
<div class="editor-label">@Html.LabelFor(x => x.Name)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Description)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Description)
@Html.ValidationMessageFor(x => x.Description)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Location)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Location)
@Html.ValidationMessageFor(x => x.Location)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Category)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Category)
@Html.ValidationMessageFor(x => x.Category)
</div>
<div class="editor-label">@Html.LabelFor(x => x.StartDate)</div>
<div class="editor-field">
@*@(Html.Telerik().DatePickerFor(m => m.StartDate))*@
@Html.EditorFor(x => x.StartDate)
</div>
<div class="editor-label">@Html.LabelFor(x => x.EndDate)</div>
<div class="editor-field">
@Html.EditorFor(x => x.EndDate)
@*@(Html.Telerik().DatePickerFor(m => m.EndDate))*@
</div>
<div class="editor-label">@Html.LabelFor(x => x.Fees)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Fees)
@Html.ValidationMessageFor(x => x.Fees)
</div>
@( Html.Telerik().ScriptRegistrar().Globalization(true))
@( Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group
.Add("telerik.common.min.css")
.Add("telerik.metro.min.css"))
)
```
#Create.cshtml
```
@{ Layout.Title = T("Create User").ToString(); }
@using (Html.BeginFormAntiForgeryPost()) {
@Html.ValidationSummary()
// Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
@Display(Model)
}
```
#Controller Snippet
```
public ActionResult Create()
{
var course = _orchardService.ContentManager.New<CoursePart>("Course");
dynamic model = _orchardService.ContentManager.BuildEditor(course);
return View((object)model);
}
[HttpPost, ActionName("Create")]
public ActionResult CreatePOST()
{
var Course = _contentManager.New("Course");
var userPart = Course.As<CoursePart>();
userPart.StartDate = _clock.UtcNow;
userPart.EndDate = _clock.UtcNow;
// Store the new user into the database
_orchardService.ContentManager.Create(Course);
dynamic model = _contentManager.UpdateEditor(Course, this);
_contentManager.Publish(Course);
_orchardService.Notifier.Information(new LocalizedString("Course has been created."));
return RedirectToAction("Index");
}
```
PartDriver.cs
namespace Orchard.Club.Drivers
{
public class CourserPartDriver : ContentPartDriver<CoursePart> {
protected override DriverResult Display(
CoursePart part, string displayType, dynamic shapeHelper)
{
return ContentShape("Parts_Course",
() => shapeHelper.Parts_Course(
Name: part.Name,
Description: part.Description));
}
//GET
protected override DriverResult Editor(CoursePart part, dynamic shapeHelper)
{
return ContentShape("Parts_Course_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/Course",
Model: part,
Prefix: Prefix));
}
//POST
protected override DriverResult Editor(CoursePart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
}
CoursePartHandler.cs
namespace Orchard.Club.Handlers
{
public class CoursePartHandler : ContentHandler
{
public class CustomerPartHandler : ContentHandler
{
public CustomerPartHandler(IRepository<CoursePartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
Filters.Add(new ActivatingFilter<CoursePart>("Course"));
OnInitializing<CoursePart>(AssignDates);
OnLoaded<CoursePart>(SetDates);
}
private void SetDates(LoadContentContext context, CoursePart part)
{
var obj = context.ContentItem.As<CoursePart>();
obj.StartDate = DateTime.Now;
obj.EndDate = DateTime.Now;
part.StartDate = DateTime.Now;
part.EndDate = DateTime.Now;
}
protected void AssignDates(InitializingContentContext context, CoursePart part)
{
var obj = context.ContentItem.As<CoursePart>();
obj.StartDate = DateTime.Now;
obj.EndDate = DateTime.Now;
part.StartDate = DateTime.Now;
part.EndDate = DateTime.Now;
}
}
}
}
@model Orchard.Club.Models.CoursePart
<fieldset>
<div class="editor-label">@Html.LabelFor(x => x.Name)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Description)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Description)
@Html.ValidationMessageFor(x => x.Description)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Location)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Location)
@Html.ValidationMessageFor(x => x.Location)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Category)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Category)
@Html.ValidationMessageFor(x => x.Category)
</div>
<div class="editor-label">@Html.LabelFor(x => x.StartDate)</div>
<div class="editor-field">
@Html.EditorFor(x => x.StartDate)
@Html.ValidationMessageFor(x => x.StartDate)
</div>
<div class="editor-label">@Html.LabelFor(x => x.EndDate)</div>
<div class="editor-field">
@Html.EditorFor(x => x.EndDate)
@Html.ValidationMessageFor(x => x.EndDate)
</div>
<div class="editor-label">@Html.LabelFor(x => x.Fees)</div>
<div class="editor-field">
@Html.EditorFor(x => x.Fees)
@Html.ValidationMessageFor(x => x.Fees)
</div>
</fieldset>
答案 0 :(得分:1)
你有:
var contentItem = _contentManager.New("Course");
_contentManager.Create(contentItem, VersionOptions.Draft);
dynamic model = _contentManager.UpdateEditor(contentItem, this);
_contentManager.Publish(contentItem);
.Create()
调用为contentItem
生成INSERT语句。然后,对该项进行的其他更改将生成UPDATE语句。
在调用.Create()
的时间点,您的对象的DateTime
字段中仍然包含零。这就是SQL Server Compact无法创建项目的原因。您需要在此之前填写默认值。