0

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

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

当我第二次将数据保存到数据库并再次查看时,数据以这种格式显示:<strong>工作描述</strong>

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

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

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

4

2 回答 2

2

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

对于创建编辑器 - 在视图页面中

 @{ 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;" })
于 2011-07-20T07:06:19.123 回答
0

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

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

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

[HttpPost]
public ActionResult Detail(MyModel myModel, string contentEditor)
{
    myModel.Description = HttpUtility.HtmlDecode(contentEditor);
    // Do stuff & save to DB
}
于 2011-04-13T08:17:20.087 回答