Search code examples
asp.netasp.net-mvcckeditor

set Model value to CKeditor in MVC Edit View


i use CKEditor in Create view for save Text to Database Model using below code

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

i use CKEDITOR.instances['Body'].setData(@Model.Body); but it not work but when i put this code in Edit view CKEditor Dont show returned model text how can i set model string field to ckeditor?


Solution

  • you can use TextAreaFor instead of 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>