Normally I just use normal @Html.BeginForm
forms and post them with ajax as per below:
$("btnSubmit").on("click", function () {
var $form = $("form#frmNewArticle");
$form.submit();
});
$("form#frmNewArticle").submit(function () {
$.post($(this).attr("action"), $(this).serialize(), function (data) {
if (!data.IsOK) {
// in the case of an error, just display info about it to the user
}
else {
// if the post went well, go back to the article listing
window.location.href = '@Url.Content("~/Articles")';
}
return;
});
return false;
});
I've been doing this with great success all over my MVC site but now I want a rich text editor, so I've now implemented tinymce
and I want to use it's inline mode which makes use of contenteditable html elements:
<h2>Article Title</h2>
<h3 class="editable"></h3>
<h2>Article Content</h2>
<div class="editable"></div>
<input type="button id="btnSubmit" value="Create" />
<script>
tinymce.init({
selector: "div.editable",
theme: "modern",
plugins: [
["advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker"],
["searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking"],
["save table contextmenu directionality emoticons template paste"]
],
add_unload_trigger: false,
schema: "html5",
inline: true,
toolbar: "undo redo | styleselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent | link image | print preview media",
statusbar: false
});
tinymce.init({
selector: "h3.editable",
theme: "modern",
add_unload_trigger: false,
schema: "html5",
inline: true,
toolbar: "undo redo",
statusbar: false
});
</script>
I've seen other questions on here that suggested making the editable elements populate actual input fields on the page which get posted but that seems a bit inefficient to me but then I'm not the most knowledgeable in this area...
In the past I have specified the exact data that gets posted (as per below), but that doesn't feel right to me either...
$.ajax({
type: 'POST',
url: '/includes/contact.php',
data: { name: $('#contact-name').val(),
from: $('#contact-email').val(),
subject: $('#contact-subject').val(),
message: $('#contact-message').val()
}, // end data
success: function clearFields() {
// clear form fields
} // end success
}); // end ajax
Can someone tell me if I'm just being pedantic about how this is done or if I'm onto something with either of these approaches?
If I'm not, how is it meant to be done?
Thanks in advance!