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

set Model value to CKeditor in MVC Edit View

我在创建视图中使用 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?

您可以使用 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>