我如何使用ACE与ASP。净MVC应用程序?

时间:2022-01-21 04:12:01

I want to use the ACE online code editor in my project. How do I use it in ASP.NET MVC?

我想在我的项目中使用ACE在线代码编辑器。我如何在ASP中使用它。净MVC吗?

I'd like to save whatever edits are made with that editor in the database. How do I do that?

我想保存数据库中使用该编辑器进行的任何编辑。我该怎么做呢?

3 个解决方案

#1


13  

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:

假设您有一个强类型模型,其中有一个名为Editor的属性,其中包含数据。现在使用普通的

加载数据:

<div id="editor"><%=Model.Editor %></div>

Now you can create an ace editor in place of the div with javascript:

现在您可以使用javascript创建一个ace编辑器来代替div:

<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
}

#2


1  

Here is a solution using most recent technologies (Razor/MVC/Ajax) :

这里有一个使用最新技术(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
    }

#3


0  

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;

#1


13  

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:

假设您有一个强类型模型,其中有一个名为Editor的属性,其中包含数据。现在使用普通的

加载数据:

<div id="editor"><%=Model.Editor %></div>

Now you can create an ace editor in place of the div with javascript:

现在您可以使用javascript创建一个ace编辑器来代替div:

<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
}

#2


1  

Here is a solution using most recent technologies (Razor/MVC/Ajax) :

这里有一个使用最新技术(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
    }

#3


0  

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;