正在創建我的第一個果園模塊,我遇到了將表單數據保存回數據庫的問題。根據我看到的大量樣本,我已經正確註冊了所有東西,所以我必須錯過一些小的事情。堅持零件記錄到數據庫
我能夠拿到公寓形式下的新菜單顯示,驗證工作,但是當我填寫表格,完全地和打救我得到:
你的公寓已創建。
檢查數據庫中的記錄是不是在該表並檢查日誌示出了:
2013年12月19日09:15:23416 [19] NHibernate.Transaction.ITransactionFactory - DTC事務prepre 階段失敗NHibernate.Exceptions.GenericADOException:不能 執行批處理命令[SQL:SQL不可用] ---> System.Data.SqlClient.SqlException:無法將值NULL插入 列'FloorPlanName',表 'Orchard.dbo.CommunityWebsiteSolutions_ApartmentPa rtRecord「; 列不允許有空值。 INSERT失敗。
正在運行SQL事件探查器顯示插入的所有列都設置爲NULL。
Migrations.cs
SchemaBuilder.CreateTable(typeof(ApartmentPartRecord).Name, table => table
.ContentPartRecord()
.Column<string>("FloorPlanName", c => c.WithLength(25).NotNull())
.Column<string>("FullAddress", c => c.WithLength(256).NotNull()))
.Column<string>("ShortDescription", c => c.WithLength(150).NotNull())
.Column("NumberOfBedrooms", DbType.Int32, c => c.NotNull())
.Column("NumberOfBathrooms", DbType.Int32, c => c.NotNull())
.Column("SquareFootage", DbType.Int32, c => c.NotNull())
.Column("WhenAvailable", DbType.DateTime)
.Column("RentAmount", DbType.Decimal)
);
ContentDefinitionManager.AlterPartDefinition(typeof (ApartmentPart).Name, part => part.Attachable());
ApartmentPart
public class ApartmentPartRecord : ContentPartRecord {
public virtual string FloorPlanName { get; set; }
public virtual string ShortDescription { get; set; }
public virtual string FullAddress { get; set; }
public virtual int? NumberOfBedrooms { get; set; }
public virtual int? NumberOfBathrooms { get; set; }
public virtual int? SquareFootage { get; set; }
public virtual DateTime? WhenAvailable { get; set; }
public virtual decimal? RentAmount { get; set; }
}
public class ApartmentPart : ContentPart<ApartmentPartRecord> {
[Required, StringLength(256)]
[Display(Name = "Address/Unit Number")]
public string FullAddress {
get { return Record.FullAddress; }
set { Record.FullAddress = value; }
}
[Required, StringLength(25)]
[Display(Name = "Floor Plan")]
public string FloorPlanName {
get { return Record.FloorPlanName; }
set { Record.FloorPlanName = value; }
}
[Required, StringLength(150)]
[Display(Name = "Sales Description")]
public string ShortDescription {
get { return Record.ShortDescription; }
set { Record.ShortDescription = value; }
}
[Required]
[Display(Name = "Bedroom Count")]
public int? NumberOfBedrooms {
get { return Record.NumberOfBedrooms; }
set { Record.NumberOfBedrooms = value; }
}
[Required]
[Display(Name = "Bathroom Count")]
public int? NumberOfBathrooms {
get { return Record.NumberOfBathrooms; }
set { Record.NumberOfBathrooms = value; }
}
[Required]
[Display(Name = "Square Footage")]
public int? SquareFootage {
get { return Record.SquareFootage; }
set { Record.SquareFootage = value; }
}
[Display(Name = "First Availability")]
public DateTime? WhenAvailable {
get { return Record.WhenAvailable; }
set { Record.WhenAvailable = value; }
}
[Display(Name = "Rent Amount")]
public decimal? RentAmount {
get { return Record.RentAmount; }
set { Record.RentAmount = value; }
}
}
驅動
public class ApartmentPartDriver : ContentPartDriver<ApartmentPart>
{
protected override string Prefix
{
get { return "Apartment"; }
}
//GET
protected override DriverResult Editor(ApartmentPart part, dynamic shapeHelper)
{
return ContentShape("Parts_Apartment_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/Apartment",
Model: part,
Prefix: Prefix));
}
//POST
protected override DriverResult Editor(ApartmentPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
處理程序
public class ApartmentPartHandler : ContentHandler {
public ApartmentPartHandler(IRepository<ApartmentPartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
}
也許我有點密集,但是當表單在Drivers Editor函數中發佈和處理時,updater.TryUpdateModel會使用POST中的所有值填充零件。之後的某些時間,值保存時會變爲NULL。我想保留記錄類的可空類型,因爲我們不會總是有一個WhenAvailable日期和RentAmount,如果我們沒有它們,我不想在這些字段中設置值。我會嘗試從列定義中刪除NotNulls,但這似乎是錯誤的。 –
你沒有用'NotNull'標記'WhenAvailable'和'RentAmount',所以這些列不會引起問題。但是其他專欄如「FloorPlanName」和「NumberOfBedrooms」呢?那些在你的記錄中是可空的類型,那麼爲什麼你在數據庫列中將它們標記爲'NotNull'? –
我已經更新了我的答案,並解釋了爲什麼您依賴TryUpdateModel的方法無效,以及爲您的內容部分提供默認值的另一種方式。 –