Reputation: 4237
I have a contentType defined, along with a driver + handler, it works fine inside of the admin page but I want to render the editor for a contentType on a cshtml page inside my module. How do I do this and can I still get the benefit of the parts being persisted etc.
Upvotes: 1
Views: 315
Reputation: 12630
You can use IContentManager.BuildEditor(...)
to produce the editor shape for a content item, and render it in your view using @Display(Model.Whatever)
.
To deal with updates, you can also use IContentManager.UpdateEditor(...)
, passing in an implementor of IUpdateModel
.
IUpdateModel
is just an Orchardy way of abstracting calls to TryUpdateModel
and AddModelError
that you find in regular ASP MVC controllers, so if you are rendering your editor from a custom controller you can implement it easily like this:
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
You can find a nice succinct example in Orchard.Blogs.Controllers.BlogAdminController
.
As an aside, you might recognise IUpdateModel
and prefix
from developing your content part driver - this abstraction is extremely versatile as it allows you to deal with multiple editors being updated at the same time (this is how stuff like content parts, fields etc have been implemented). It allows you to do some cool stuff like edit multiple content items on the same page. We use this at my work to implement a custom forms editor which has some nice features like drag and drop design etc.
Upvotes: 1