gpt4 book ai didi

c# - 如何在 mvc5 中为 Action 使用 allowhtml 属性

转载 作者:太空狗 更新时间:2023-10-29 16:51:28 25 4
gpt4 key购买 nike

我正在开发一个 MVC 5 项目,我想使用 CKEditor用于输入数据。此数据保存为 HTML,但当我尝试显示它时,出现错误。 See code

最佳答案

您可以将 AllowHtml 特性应用于在您的 View 模型类中保存标记的属性。

public class CreatePost
{
public string PostTitle {set;get;}
[AllowHtml]
public string PostContent { set;get;}
}

并在您的 HttpPost 操作方法中使用此 View 模型,一切都会正常工作。

[HttpPost]
public ActionResult Create(CreatePost viewModel)
{
// Check viewModel.PostContent property
// to do : Return something
}

现在只需确保您正在使用此属性来构建要与 CKEditor 一起使用的文本区域

@model CreatePost
@using (Html.BeginForm())
{
@Html.TextBoxFor(s => s.PostTitle)
@Html.TextAreaFor(s=>s.PostContent)
<input type="submit" />
}
@section Scripts
{
<script src="//cdn.ckeditor.com/4.5.9/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('Message');
</script>
}

关于c# - 如何在 mvc5 中为 Action 使用 allowhtml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37886474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com