-
Notifications
You must be signed in to change notification settings - Fork 0
Saving, Updating and Deleting Content
In order to Create, Update or Delete Content in Umbraco, Concrete adds some generic extension methods to the ContentService that work with Concrete Models. You will need to add the following using statement in order to access them:
using ConcreteContentTypes.Core.Extensions;
Most of the extension methods provided are overloads for existing methods on the ContentService for simplicity:
T CreateContentWithIdentity<T>(string name, int parentId, int userId = 0)
T CreateContentWithIdentity<T>(string name, IContent parent, int userId = 0)
T CreateContentWithIdentity<T>(string name, IConcreteModel parent, int userId = 0)
void Save<T>(T model, int userId = 0, bool raiseEvents = true)
Attempt<Umbraco.Core.Publishing.PublishStatus> SaveAndPublishWithStatus<T>(T model, int userId = 0, bool raiseEvents = true)
void Delete<T>(T model, int userId = 0)
Both the Save and SaveAndPublishWithStatus methods will create a content item for you if the model passed does not represent an existing content item.
There is one entirely new method that allows the properties from a Concrete Model to be mapped to an IContent object:
void MapModelToIContent<T>(T model, IContent content)
This will call the IContent object's SetValue method for all basic clr type properties on the model (int, string, DateTime etc). Currently only persisting of basic clr types is supported, complex types will be ignored. This method is provided in case you want to set more than the basic clr properties as all the other methods don't provide direct access to the IContent object.
In the sandbox project in the repository I have created an example of using Concrete to create Blog Comments.
BlogPost.cshtml
The BlogPost view calls a SurfaceController that renders a partial view containing a form for creating comments:
@Html.Action("RenderSubmitCommentForm", "BlogPost", new { blogPost = Model })
Here the Model property is a BlogPost object, we pass this to the RenderSubmitCommentForm method on the BlogPostController.
BlogPostController.cs
[ChildActionOnly]
public ActionResult RenderSubmitCommentForm(BlogPost blogPost)
{
var blogComment = new BlogComment("Blog Comment " + DateTime.Now.ToString(), blogPost);
return PartialView("BlogSubmitComment", blogComment);
}
The RenderSubmitCommentForm method takes the BlogPost object we passed in through the view as an input parameter. We then create a new BlogComment object, giving a name and passing the BlogPost object as its parent.
We then return a PartialView result passing in the BlogComment object we just created as the model.
BlogSubmitComment.cshtml
On the partial view for the form we specify the model as BlogComment.
I am using an Ajax form (not very well!) to submit the comments to a WebApi controller. We can use the HtmlHelper methods to create our form fields as the model is set a BlogComment. The Mvc unobtrusive validation is enabled in the project so the [Required] attributes added to the properties of the BlogComment class mean that no additional work to get basic validation on the form is needed. See more about this here.
@model BlogComment
<script type="text/javascript">
function CommentSubmitSuccess() {
$("#commentSubmitForm").hide();
$("#commentSuccess").show();
}
</script>
<div id="commentSubmitForm">
@using (Ajax.BeginForm(new AjaxOptions()
{
Url = "/umbraco/api/BlogCommentApi/SubmitBlogComment",
HttpMethod = "Post",
OnSuccess = "CommentSubmitSuccess"
}))
{
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
@Html.HiddenFor(x => x.ParentId)
<label>Your Name</label>
@Html.TextBoxFor(x => x.FullName)
@Html.ValidationMessageFor(x => x.FullName)
<br /><br />
<label>Comment</label>
@Html.TextAreaFor(x => x.Comment);
@Html.ValidationMessageFor(x => x.Comment)
<br /><br />
<input type="submit" value="Submit" />
}
</div>
<div id="commentSuccess" style="display:none">
Thanks for your comment!
</div>
BlogCommentApiController.cs
This is the WebApi controller that handles the post from the form. The SubmitBlogComment method handles the post:
[HttpPost]
public bool SubmitBlogComment(BlogComment comment)
{
var result = Services.ContentService.SaveAndPublishWithStatus(comment);
return result.Success;
}
This method has been made very simple. It takes a BlogComment object as an input parameter. The name and parent were set when the BlogComment object was created so all we have to do is call SaveAndPublishWithStatus method on the BlogComment. Until this point, nothing has been added to the database. SaveAndPublishWithStatus works out this is a new object and creates it before Publishing. We then simply return the result of the creation to the Client.