【问题标题】:Deleting relational Non-Content data from Orchard CMS从 Orchard CMS 中删除相关的非内容数据
【发布时间】:2012-07-18 09:14:43
【问题描述】:

我通过定义这样的记录和架构在 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"));
    }
}

然后我有两个控制器来处理这两个实体的管理页面。在国家控制器中,我有以下操作:

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

有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 为什么没有给出理由就投反对票???

标签: asp.net-mvc asp.net-mvc-3 model-view-controller orchardcms


【解决方案1】:

这是因为您的DeletePOST() 参数是CountryRecord。 Orchard 记录全部由 NHibernate 框架代理,MVC 的 ModelBinder 无法为您正确创建它们。

您需要做的就像您在非 POST 方法中所做的一样:只接受 CountryRecord 的整数 ID,从存储库中获取新的记录,然后将其删除。

【讨论】:

  • 糟糕,刚刚在 Codeplex 上搜索了您的问题,发现@pszmyd 已经发布了相同的解决方案。哦,好吧,很高兴我们的想法相同 :) orchard.codeplex.com/discussions/370770
  • 哈哈,谢谢!我忘了用答案更新这个问题:(谢谢,这是正确的答案,我也会标记它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多