【问题标题】:ASP.NET MVC 2 Telerik Editor not displaying formatted textASP.NET MVC 2 Telerik Editor 不显示格式化文本
【发布时间】:2011-04-13 07:53:09
【问题描述】:

我在使用 Telerik MVC 编辑器控件时遇到问题,它允许我输入数据、正确格式化(粗体等)并将其保存到数据库中。当我在浏览器中查看数据时,它会按预期显示(在所有浏览器中)。

当我尝试在编辑器中再次编辑文本时,它并没有正确显示格式,而是显示文本周围的 HTML 标记,即 working 与格式化文本相反 工作。

当我第二次将数据保存到数据库并再次查看时,数据以这种格式显示:working description

这是我用来显示文本编辑器的代码:

<% Html.Telerik().Editor()
    .Name("Description")
    .Value(Model.Description)
    .Render();
%>

// Code to the populate the model before saving to the database: There is no endcode or decode instruction here
article.Description = collection["Description"];
// Save changes.

要在浏览器中显示代码,我使用以下代码:

<%=
    HttpUtility.HtmlDecode(Model.Description)
%>

希望这个解释是有道理的,有人可以帮助阐明这一点吗?我真的很困惑如何使它正常工作。

【问题讨论】:

  • 刚刚发现一个错误,粗体的第一个工作实际上显示为 working 在第三段文本显示为 working 文本实际上显示为<strong>工作描述</strong>堆栈溢出中的自动格式化对我来说太智能了,我在标记代码块时错过了它们!

标签: asp.net-mvc-2 editor telerik telerik-mvc


【解决方案1】:

我正在使用ASP.NET MVC 3 和 Telrik 控件,我使用了下面的代码,它工作正常。

用于创建编辑器 - 视图页面

 @{ Html.BeginForm(); }
            @Html.Telerik().EditorFor(model => model.Body).HtmlAttributes(new { style = "height:200px; width:500px;" })
            @Html.ValidationMessageFor(model => model.Body)

@{ Html.EndForm(); }

在控制器中

 public ViewResult Details(int id)
        {
            ArticleModels articlemodels = db.ArticleModels.Find(id);
            articlemodels.Body = HttpUtility.HtmlDecode(articlemodels.Body);
            return View(articlemodels);
        }

在视图中编辑

@Html.Telerik().EditorFor(model => model.Body).HtmlAttributes(new { style = "height:200px; width:500px;" })

【讨论】:

    【解决方案2】:

    我让它为我工作,希望它对你有用。 在我看来,我使用这个代码:

    <%: Html.Telerik().EditorFor(model => model.Description).Name("ContentEditor")%>
    

    然后在控制器中,我将值传递到模型之外(因为Model.Description属性始终为null,我还不知道为什么),像这样:

    [HttpPost]
    public ActionResult Detail(MyModel myModel, string contentEditor)
    {
        myModel.Description = HttpUtility.HtmlDecode(contentEditor);
        // Do stuff & save to DB
    }
    

    【讨论】:

    • 嗨,Thomas,感谢您回复我,我试过了,但不幸的是仍然遇到同样的问题。我认为是时候尝试使用 Telerik 版本以外的其他编辑器了,可惜
    • 如果你可以切换到另一个编辑器,那就去做吧。 Telerik MVC 组件还没有准备好,就像它们处于某种公开测试版中一样。如果您尝试在他们网站上提供的示例之外的其他内容,您有 50% 的机会会发现错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多