在MVC编辑视图中将模型值设置为CKeditor

时间:2018-06-02 05:38:10

标签: asp.net asp.net-mvc ckeditor

我在创建视图中使用CKEditor,使用下面的代码

将文本保存到数据库模型
<div class="row">
        <div class="col">
            @Html.LabelFor(model => model.Body, htmlAttributes: new { @class = "control-label col-md-2" })
        </div>
        <div class="col">
            @Html.EditorFor(model => model.Body, new { htmlAttributes = new { @class = "form-control" } })
            <script>
                CKEDITOR.replace("Body", { htmlEncodeOutput: true });

            </script>
            @Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
        </div>
    </div>

我使用CKEDITOR.instances['Body'].setData(@Model.Body);但它不起作用 但是当我把这段代码放在编辑视图中时,CKEditor不会显示返回的模型文本 如何将模型字符串字段设置为ckeditor?

1 个答案:

答案 0 :(得分:0)

您可以使用TextAreaFor而不是EditorFor:

<div class="row">
    <div class="col">
        @Html.LabelFor(model => model.Body, htmlAttributes: new { @class = "control-label col-md-2" })
    </div>
    <div class="col">
        @Html.TextAreaFor(model => model.Body, new { htmlAttributes = new { @class = "form-control" } })
        <script>
            CKEDITOR.replace("Body", { htmlEncodeOutput: true });

        </script>
        @Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
    </div>
</div>