Interface IUpdateModel - Controller implementation cannot save the content type with ContentManager.UpdateEditor

Topics: Core, Customizing Orchard
Nov 5, 2012 at 8:38 AM

I wrote a content type called "Suggestion" following the way that the "BlogPost" is written. The code samples below are copied from BlogPostAdmnController.cs, with deletion of the authentication codes.

What I was doing:

1. I use the following code to render the eidt view:

 

public ActionResult Create(int blogId) {

    var blog = _blogService.Get(blogId, VersionOptions.Latest).As<BlogPart>();
    if (blog == null)
        return HttpNotFound();

    var blogPost = Services.ContentManager.New<BlogPostPart>("BlogPost");
    blogPost.BlogPart = blog;

    dynamic model = Services.ContentManager.BuildEditor(blogPost);
    return View((object)model);
}

 

2. When I click save button on the eidt view, the following code will catch the post and continue to execute:

 

[HttpPost, ActionName("Create")]
[FormValueRequired("submit.Save")]
public ActionResult CreatePOST(int blogId) {
    var blog = _blogService.Get(blogId, VersionOptions.Latest).As<BlogPart>();

    if (blog == null)
        return HttpNotFound();

    var blogPost = Services.ContentManager.New<BlogPostPart>("BlogPost");
    blogPost.BlogPart = blog;
            
    Services.ContentManager.Create(blogPost, VersionOptions.Draft);
    var model = Services.ContentManager.UpdateEditor(blogPost, this);

    Services.Notifier.Information(T("Your {0} has been created.", blogPost.TypeDefinition.DisplayName));
    return Redirect(Url.BlogPostEdit(blogPost));
}

//My code has issue with this method
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
    return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

 

What I've found:

1. I have examined that, to create a blog post, the IUpdateModel.TryUpdateModel method are called iteratively, with different models and prefix as parameters. And the blog post can be created successfully.

2. However, to use the same logic to create my own content type, the IUpdateModel.TryUpdateModel method are called in an infinitive loop, and it always has the same model and prefix as parameters like below:

model : {OwnerEditorViewModelProxyc70e31abdb3e47ea8a4e6d3fa208aa9c}, prefix: OwnerEditor"

Finally, I got the following Exception:

An unhandled exception of type 'System.StackOverflowException' occurred in InnoCellence.iSuggest.dll

Not sure what's wrong with my code, any essential part that I have missed? Many many thanks.

Coordinator
Nov 6, 2012 at 1:54 AM

Please put a breakpoint in there, attach a debugger and follow the execution. You will at least get a stack trace that would help understand what's wrong here.

Nov 7, 2012 at 3:52 AM
Edited Nov 7, 2012 at 3:53 AM

Thank you bertrandleroy for your update. Finally, I found what's wrong with my code.

I implicitly implemented the interface IUpdateModel, as below. And it always leads to the execution of TryUpdateModel in an infinitive loop.

public new bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
    return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

public void AddModelError(string key, Orchard.Localization.LocalizedString errorMessage) {
    throw new NotImplementedException();
}

Now, I am using explicit implementation of the IUpdateModel, and the issue gone now.