Implementing TinyMCE
into ASP.NET MVC
encountered a problem uploading images .
The form I'm trying to implement is as follows:
I have a View
with Script
that calls a Controller
containing a upload method that does its job saving the image to my server, as it is a common method and is working perfectly, I left it in the Gist , in order to shorten the question a little. And my View
:
@model EuVotoAf.Models.Publicacao
@{
ViewBag.Title = "Create";
}
<script src="~/Scripts/tinymce/tinymce.js"></script>
<h2>Create</h2>
<iframe id="form_target" name="form_target" style="display:none"></iframe>
@using (Html.BeginForm("Upload", "Publicacao", FormMethod.Post, new { @id = "my_form", target = "form_target", enctype = "multipart/form-data", style = "width:0;height:0;overflow:hidden" }))
{
@Html.AntiForgeryToken()
<input name="file" type="file" onchange="$('#my_form').submit();this.value='';">
}
<script type="text/javascript">
tinymce.init({
selector: "textarea",
theme: "modern",
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor colorpicker textpattern imagetools"
],
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
toolbar2: "print preview media | forecolor backcolor emoticons | ltr rtl",
image_advtab: true,
templates: [
{ title: 'Test template 1', content: 'Test 1' },
{ title: 'Test template 2', content: 'Test 2' }
],
file_browser_callback: function (field_name, url, type, win) {
if (type == 'image') $('#my_form input').click();
}
});
</script>
@Html.TextAreaFor(model => model.Conteudo, new { id = "my_editor", @class = "mceEditor" })
@using (Html.BeginForm("Create", "Publicacao", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
What I basically need is to send the text to be saved in DB
and the image to be saved on the server. If I put the line
@Html.TextAreaFor(model => model.Conteudo, new { id = "my_editor", @class = "mceEditor" })
Within the first form
it does not render on the screen, if I put it in the second, the upload is not done. Which is the way I can make it work in some way.
Am I doing the wrong thing?
That's right, but does it contain errors in the code?
Is there a better way to do it?
If you have any other important details that I did not enter, I can put it.