1

我已經宣佈在果園CMS一些非內容數據的定義是這樣的記錄和模式:從果園CMS中刪除相關的非內容數據

public class CountyRecord 
{ 
    public virtual int Id { get; set; } 
    public virtual string CountyName { get; set; } 
    public virtual CountryRecord CountryRecord { get; set; } 
} 

public class CountryRecord 
{ 
    public CountryRecord() 
    { 
     CountyRecords = new List<CountyRecord>(); 
    } 
    public virtual int Id { get; set; } 
    public virtual string CountryName { get; set; } 
    public virtual IList<CountyRecord> CountyRecords { get; set; } 
} 

public class Migrations: DataMigrationImpl 
{ 
    public int Create() 
    { 
     //COUNTIES 
     SchemaBuilder.CreateTable(typeof(CountyRecord).Name, table => table 
      .Column<int>("Id", col => col 
       .PrimaryKey() 
       .Identity()) 
      .Column<string>("CountyName") 
      .Column<int>("CountryRecord_Id")); 

     //COUNTRIES 
     SchemaBuilder.CreateTable(typeof(CountryRecord).Name, table => table 
      .Column<int>("Id", col => col 
       .PrimaryKey() 
       .Identity()) 
      .Column<string>("CountryName")); 
    } 
} 

然後我有兩個控制器處理的管理頁面這兩個實體。在國家控制我有下列行爲:

//DELETE 
    [HttpGet, Admin] 
    public ActionResult Delete(int countryId) 
    { 
     var country = CountryRepo.Get(countryId); 
     if (country == null) 
     { 
      return new HttpNotFoundResult("Couldn't find the country with ID " + countryId.ToString()); 
     } 
     return View(country); 
    } 

    [HttpPost, Admin, ActionName("Delete")] 
    public ActionResult DeletePOST(CountryRecord country) 
    {  
     foreach (CountyRecord county in CountyRepo.Fetch(c=>c.CountryRecord.Id==country.Id)) 
     { 
      CountyRepo.Delete(county); 
     } 

     CountryRepo.Delete(country); 
     OrchardServices.Notifier.Add(NotifyType.Information, T("Country '{0}' deleted successfully", country.CountryName)); 
     return RedirectToAction("Index"); 
    } 

這是與雲的觀點:

@model Addresses.Models.CountryRecord 
<div class="manage"> 
@using (Html.BeginFormAntiForgeryPost("Delete")) 
{ 
    <h2>Are you sure you want to delete this country and ALL its counties?</h2> 
    @Html.HiddenFor(m => m.Id); 
    @Html.HiddenFor(m => m.CountryName); 
    @Html.ActionLink(T("Cancel").Text, "Index", "CountriesAdmin", new { AreaRegistration = "Addresses" }, new { style = "float:right; padding:4px 15px;" }) 
    <button class="button primaryAction" style="float:right;">@T("Confirm")</button> 
} 
</div> 

然而,這裏的問題,當我刪除仍然有一個國家分配給它的縣,它引發以下錯誤:

a different object with the same identifier value was already associated with the session 

任何人都可以請幫助?

謝謝。

+0

爲什麼downvote沒有理由??? – hofnarwillie 2012-09-17 08:28:02

回答

2

這是因爲您的DeletePOST()參數是CountryRecord。果園記錄都是由NHibernate框架代理的,MVC的ModelBinder無法爲你正確創建它們。

你需要做的,而不是什麼像什麼,你在非POST方法做:接受只是CountryRecord的整數ID,從存儲庫中取出的記錄新鮮,然後刪除。

+0

糟糕,剛剛在Codeplex上搜索了您的問題,並意識到@pszmyd已經發布了相同的解決方案。噢,很高興我們都這麼認爲:) http://orchard.codeplex.com/discussions/370770 – 2012-07-23 23:51:18

+0

哈哈,謝謝!我忘了更新這個問題的答案:(謝謝,但這是正確的答案,我會標記它。 – hofnarwillie 2012-07-24 07:53:06