1

次のようにレコードとスキーマを定義することにより、Orchard 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"));
    }
}

次に、これら 2 つのエンティティの管理ページを処理する 2 つのコントローラーを用意します。国コントローラーでは、次のアクションがあります。

    //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

誰でも助けてもらえますか?

ありがとう。

4

1 に答える 1

2

DeletePOST()パラメータがCountryRecord. Orchard レコードはすべて NHibernate フレームワークによってプロキシされ、MVC の ModelBinder はそれらを適切に作成できません。

代わりに行う必要があるのは、非 POST メソッドで行うことと同様です。CountryRecord の整数 ID のみを受け入れ、リポジトリから新しいレコードを取得してから削除します。

于 2012-07-23T23:40:28.777 に答える