0

I am using a Kendo editor to create email templates and on POST, once a change to the template has been submitted, always renders in encoded HTML.

This is my razor code on the page:

@model Business.Models.Administration.EmailSetupViewModel
@using Kendo.Mvc.UI;

<h2>Application Stages Portal</h2>

<h4>Email Setup</h4>

@using (Html.BeginForm())
{

    if (Model.EmailSaved)
    {
        <h2>
            Email template saved</h2>
    }
    else
    {
       @* @Html.DisplayFor(m => m.EmailSavedMsg)*@
    }

    @Html.DropDownListFor(m => m.EmailTemplateToEdit, Model.EmailTemplatesList)
    <input type="submit" name="setup" value="setup" />

    if (Model.ShowEmailForm)
    {
    <div id="email-edit">
        @Html.Label("Title")
        @Html.TextBoxFor(m => m.EmailTitle, new { style = "width:200px" })
        <br />

        @(Html.Kendo().Editor()
      .Name("editor")
      .HtmlAttributes(new { style = "width: 600px;height:440px" })
      .Value(@<text>
        @Html.Raw(Model.EmailBody)
        </text>))
    </div>

    <input type="submit" id="btnSaveTemplate" name="update" value="update" />

    <h2>
        Please note</h2>
    <p>
        The following items are <i>reserved and should not be changed, you may move them
            to a different place within the message. </i>

        <ul>
            <li><*name*> e.g. Fred Flinstone </li>
            <li><*membernumber*> e.g. 12345678 </li>
        </ul>
    </p>
    }
} 

And this is where the actual editor markup is on the page

 @(Html.Kendo().Editor()
          .Name("editor")
          .HtmlAttributes(new { style = "width: 600px;height:440px" })
          .Value(@<text>
            @Html.Raw(Model.EmailBody)
            </text>))

Model.EmailBody contains the actual string.

When I GET the page, it renders fine. But when I do POST it never decodes so the rendering is wrong. I don't want to see all the HTML tags but the actual formatting.

This is my Controller code:

#region Email template
        [HttpGet]
        public ActionResult EmailSetup()
        {
            ViewBag.DisplayName = StaticFunctions.GetDisplayName(this.User.Identity.Name);
            EmailSetupViewModel model = new EmailSetupViewModel();
            Business.Administration.Email Email = new Business.Administration.Email();
            var list = Email.GetTemplateList();
            model.EmailTemplatesList = list.OrderBy(o => o.Text).ToList();
            return View(model);
        }

        [HttpPost]
        public ActionResult EmailSetup(EmailSetupViewModel model, string value, string editor)
        {
            ViewBag.DisplayName = StaticFunctions.GetDisplayName(this.User.Identity.Name);
            string body = HttpUtility.HtmlDecode(editor); //encode to db

            if (Request["update"] != null)
            {
                Business.Administration.Email Email = new Business.Administration.Email();
                model.EmailSaved = Email.SaveTemplate(model, body);
                //ModelState.Clear(); // when doing POST - clearing the ModelState will prevent encode of HTML (Default behaviour). This isn't good long term solution.

                if (model.EmailSaved)
                {
                    model.EmailSavedMsg = "Template saved";
                }
                else
                {
                    model.EmailSavedMsg = "Template couldn't be saved";
                }

                model.EmailTemplatesList = Email.GetTemplateList();
                model = Email.GetTemplate(model);
                model.EmailBody = HttpUtility.HtmlDecode(model.EmailBody);
                return View(model);
            }
            else
            {
                Business.Administration.Email Email = new Business.Administration.Email();
                model.EmailTemplatesList = Email.GetTemplateList();
                model = Email.GetTemplate(model);
                model.EmailBody = HttpUtility.HtmlDecode(model.EmailBody);
                return View(model);
            }
        }
        #endregion

This is my model, I am using [AllowHtml] attribute on the property.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc;

namespace Business.Models.Administration
{
    public class EmailSetupViewModel
    {
        public EmailSetupViewModel()
        {
            this.EmailTemplatesList = new List<SelectListItem>();
        }

        public string EmailTemplateToEdit { get; set; }
        public List<SelectListItem> EmailTemplatesList { get; set; }
        public string EmailTitle { get; set; }
        [AllowHtml]
        public string EmailBody { get; set; }
        public bool ShowEmailForm { get; set; }

        public bool EmailSaved { get; set; }
        public string EmailSavedMsg { get; set; }
    }
}

Finally two screenshots, one on GET and one on POST.

enter image description here

enter image description here

I was using ModelState.Clear() as well but when I clicked back on the browser, it wouldn't decode.

So basically I want help rendering the HTML in my editor on post so it renders properly and doesn't show HTML tags in the editor.

tereško
  • 58,060
  • 25
  • 98
  • 150
nick gowdy
  • 6,191
  • 25
  • 88
  • 157

0 Answers0