public class PostController : SurfaceController
{
// GET: Post
[HttpGet]
public JsonResult PostComment(string name, string subject, string message, int id)
{
try
{
var contentService = Services.ContentService;
var roort = Umbraco.TypedContentAtRoot().First();
var contactNode = roort.Children.First(x => x.DocumentTypeAlias == "blog");
var postNode = contactNode.Descendants().First(x => x.DocumentTypeAlias == "blogpost");
// var node = contentService.GetById(1181);
var cont = contentService.CreateContent("comment", id, "postcomment");
//var cont = contentService.CreateContent("comment1", id, "comments");
cont.SetValue("firstname", name);
cont.SetValue("subject", subject);
cont.SetValue("message", message);
cont.SetValue("isapprove", false);
// cont.SetValue("message", model.Message);
contentService.SaveAndPublishWithStatus(cont);
return Json(new { success = true, message = message }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { success = false, message = "Failed to create node and add data" }, JsonRequestBehavior.AllowGet);
}
}
<script>
$(document).ready(function () {
$("#Form").on('click', function (e) {
debugger;
var name = $("#name").val();
var subject = $("#subject").val();
var message = $("#message").val();
var id = @Model.Content.Id;//$("#id").val();
$.ajax({
url: "/umbraco/surface/Post/PostComment",
type:"Get",
dataType: "json",
data: { 'name': name, 'subject': subject, 'message': message, 'id':id },
cache: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.success) {
alert(result.message);
}
alert(result.message);
window.location.reload();
},
failure: function(result) {
alert(result.message);
window.location.reload();
}
});
});
});
</script>
No comments:
Post a Comment