Proper way to create content item with AutoroutePart?

Topics: Customizing Orchard
Jul 29, 2013 at 4:22 PM
I have a module that I use custom controller to create new content items. Something like below
var product = _contentManager.New("Product");
var model _contentManager.UpdateEditor(product, this);
if ( !ModelState.IsValid ) {
  return View(model);
}
_contentManager.Create(product);
The Autoroute default pattern is /products/{Content.Id}

The issue is the saved route is always /products/0.

My guess is because when calling UpdateEditor, the AutoroutePart driver is call as well that causes the route to generate when Content.Id is still 0.

Is it the normal behavior? I don't really want to create the object in database first and then updating it... Is there better way to handle this?

Thanks!
Jul 29, 2013 at 6:40 PM

I don't really want to create the object in database first and then updating it... Is there better way to handle this?

I guess there's no better way since the DB assigns the ID - you won't know it before you save your content item. But don't worry too much about that - if you did you might find yourself prematurely optimizing which you shouldn't. Content item creation is usually a rather rare activity compared to all the reading of DB entries.

--


2013/7/29 awyl <[email removed]>

From: awyl

I have a module that I use custom controller to create new content items. Something like below
var product = _contentManager.New("Product");
var model _contentManager.UpdateEditor(product, this);
if ( !ModelState.IsValid ) {
  return View(model);
}
_contentManager.Create(product);
The Autoroute default pattern is /products/{Content.Id}

The issue is the saved route is always /products/0.

My guess is because when calling UpdateEditor, the AutoroutePart driver is call as well that causes the route to generate when Content.Id is still 0.

Is it the normal behavior? I don't really want to create the object in database first and then updating it... Is there better way to handle this?

Thanks!

Read the full discussion online.

To add a post to this discussion, reply to this email ([email removed])

To start a new discussion for this project, email [email removed]

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jul 29, 2013 at 7:14 PM
Thanks for you reply.

I just feel it is weird. Because if I am not using UpdateEditor, New and Create work...
Jul 29, 2013 at 7:22 PM
Also there will be an exception if a field in database is set to NotNull() when using Create directly, which I was forced to remove it because of this Auotoroute issue...

Thanks.
Jul 29, 2013 at 9:58 PM
Because if I am not using UpdateEditor, New and Create work...

I'm not sure what exactly you mean by "New and Create" work - your AutoroutePart should not be populated then. But that wouldn't be of much use, would it?

Also, I can't follow which NotNull() field you're talking about. In any case, you could just temporarily save away the first (wrong) route value, and then update the route and save again. You just need to set
AutomaticAdjustmentOnEdit=true
on the AutoroutePart.

Then you should be able to do something like this:

var product = _contentManager.New("Product");
var model = _contentManager.UpdateEditor(product, this);
if ( !ModelState.IsValid ) {
  return View(model);
}
_contentManager.Create(product);
// You need to somehow call into the AutoroutePartDriver.Editor method to set the route again
// but I am not sure if this will actually do what you want
_contentManager.UpdateEditor(product, this);
The changes will automatically be saved at the end of the request.
--


2013/7/29 awyl <[email removed]>

From: awyl

Also there will be an exception if a field in database is set to NotNull() when using Create directly, which I was forced to remove it because of this Auotoroute issue...

Thanks.

Read the full discussion online.

To add a post to this discussion, reply to this email ([email removed])

To start a new discussion for this project, email [email removed]

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jul 29, 2013 at 10:36 PM
Sorry, I wasn't clear.

In migrations.cs, for example,
SchemaBuilder.CreateTable("ProductPartRecord", table => table
  ...
  .Column<string>("Description", column => column.NotNull().WithLength(500) )
);
would cause _contentManager.Create<ProductPart>("Product") to throw an exception.

I have another place to create a product from fetching an xml feed. This service does not use UpdateEditor, my code is something like,
var p = _contentManager.New<ProductPart>("Product");
p.Description = feed.Description;
_contentManager.Create(p);
AutoroutePart is created properly.

I do have AutomaticAdjustmentOnEdit=true set.

Thanks for you answers.
I settled with Create the object first for simplicity.