Create/update tags for a content item

Topics: Customizing Orchard, Writing modules
Jul 12, 2011 at 3:50 PM

Hi all,

please advice why the following code:

        [Admin]
        [HttpPost]
        [ActionName("Create")]
        public ActionResult CreatePOST()
        {
            // create department content item
            var department = orchardServices.ContentManager.New("Department");
            // persist (?) a content item
            orchardServices.ContentManager.Create(department);
            // update department content item parts through drivers, changes will be automatically propagated to the DB
            var model = orchardServices.ContentManager.UpdateEditor(department, this);
           
            if (!ModelState.IsValid)
            {
                orchardServices.TransactionManager.Cancel();
                return View("Edit", (object)model);
            }

            return RedirectToAction("Create");
        }

throws the "null id in Orchard.Tags.Models.TagRecord entry" exception? This is not clear for, me because similar code works fine:

        [HttpPost]
        [Admin]
        [ActionName("Edit")]
        public ActionResult EditPOST(int id)
        {
            // create department content item
            var department = orchardServices.ContentManager.Get(id);
            // update department content item parts through drivers
            var model = orchardServices.ContentManager.UpdateEditor(department, this);

            if (!ModelState.IsValid)
            {
                orchardServices.TransactionManager.Cancel();
                return View("Edit", (object)model);
            }

            return RedirectToAction("Create");            
        }

So, TagsPart is a part of my content type Department - i see the appropriate tags text box, and the EditPOST method works as expected, unlike the CreatePOST. Possibly I misunderstand some of Orchard concepts..

Thanks,
Paul Borovik

 

Coordinator
Jul 12, 2011 at 8:01 PM

This looks like it should work, although I don't understand why you would be checking the model state in that situation.

Jul 15, 2011 at 11:52 AM

Well, this behavior is clear seems to be clear for me now:

UpdateEditor call makes the TagsPartDriver save a new tag record via repository Create call. It seems like NHibernate makes a flush at this moment - departmen's state is synchronized with DB as well. The reason of the issue was in the following: my Department part (record) has a CreationDate field of DateTime type (not nullable). At the moment of flush CreationDate was still initialized with default value which has been rejected by the DB engine - this causes an exception that prevents committing a transaction at the end of request. After making CreationDate nullable the module works nice.
Regarding to this I have three questions:

  • is there any way to keep my domain model (implemented via records) consistent - e.g. if I need not-nullable DateTime values?
  • why does not Orchard add a foreign key constraint for  links between records (defined via .AddColumn<int>("XXX_id"))?
  • why should not I check model state in samples above? I know drivers could add a model error, so I should handle this.. Could you clarify your point please?

Thanks!

Coordinator
Jul 15, 2011 at 7:02 PM

1. I think you'll have to set some default values, if the column is not nullable.

2. You can create constraints as part of the migration.

3. I got confused, my mistake.