I want to use the ACE online code editor in my project. How do I use it in ASP.NET MVC?
I\'d like to save whatever edits are made with that editor in the database. How d
Let's assume you have a strong typed model with a property called Editor
with the data in it. Now use a normal <div>
to load the data:
<div id="editor"><%=Model.Editor %></div>
Now you can create an ace editor in place of the div with javascript:
<script src="src/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
window.onload = function() {
var editor = ace.edit("editor");
};
</script>
Now when you want to save the data, for instance via a form post, use something like this to bind it back to the Editor
property of the model:
<%=Html.HiddenFor(m=>m.Editor, new { @id = "hidden_editor" }) %>
<!-- this is jQuery, but you can use any JS framework for this -->
<script>
$("form").submit(function () {
$("#hidden_editor").val(editor.getSession().getValue());
});
</script>
In your controller you can now save the data to the database
[HttpPost]
public ActionResult Index (IndexModel model) {
var data = model.Editor;
// save data in database
}
Here is a solution using most recent technologies (Razor/MVC/Ajax) :
$(document).ready(function() {
$("#btnSave").on("click", function () {
$.ajax({
url: '@Url.Action("YourAction", "YourController")',
type: 'POST',
data: { id: @Model.ID,
html: ace.edit("editor").getValue() },
cache: false,
success: function (response) {
alert("Changes saved.");
}
});
});
});
In Controller :
[AjaxAuthorize]
[HttpPost, ValidateInput(false)]
public ActionResult YourAction(string id, string html)
{
if (id == null || String.IsNullOrEmpty(id))
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
// do you stuff
}
This is how I ended up doing it in Razor Views
@model OfficeGx.Cms.Model.ClassName
<div class="form-group row">
<div class="col-lg-11">
@Html.HiddenFor(x => x.CascadingStylesHdn, new { @id = "hidden_cssEditor" })
@Html.LabelFor(x=>x.CascadingStyles)
<div id="cssEditor">@Model.CascadingStyles</div>
</div>
</div>
<div class="form-group row">
<div class="col-lg-11">
@Html.HiddenFor(x => x.JavaScriptHdn, new { @id = "hidden_jsEditor" })
@Html.LabelFor(x => x.JavaScript)
<div id="jsEditor">@Model.JavaScript</div>
</div>
</div>
<script>
var cssEditor;
var jsEditor;
window.onload = function() {
cssEditor = ace.edit("cssEditor");
cssEditor.getSession().setMode("ace/mode/css");
cssEditor.setTheme("ace/theme/twilight");
jsEditor = ace.edit("jsEditor");
jsEditor.getSession().setMode("ace/mode/javascript");
jsEditor.setTheme("ace/theme/xcode");
};
$("form").submit(function () {
$("#hidden_cssEditor").val(window.cssEditor.getSession().getValue());
$("#hidden_jsEditor").val(window.jsEditor.getSession().getValue());
});
</script>
<style>
#cssEditor, #jsEditor {
position: relative;
height: 400px
}
@Model.CascadingStyles
</style>
In my Controller Add/Edit Method
[HttpPost]
[ValidateInput(false)]
public ActionResult AddEdit(Article article, FormCollection formCollection)
{
article.CompanyId = OperatingUser.CompanyId;
article.CascadingStyles = article.CascadingStylesHdn;
article.JavaScript = article.JavaScriptHdn;