如何在mvc5中使用allowhtml属性进行操作

jav*_*vad 4 html c# asp.net-mvc action insert

我正在开发一个mvc 5项目,我想使用ckEditor输入数据,所以在这个编辑器中
,我可以在插入数据之后保存数据,但是当它显示错误时 看到Imaage

Shy*_*yju 12

您可以将AllowHtml属性应用于在视图模型类中包含标记的属性.

public class CreatePost
{
  public string PostTitle {set;get;}
  [AllowHtml]
  public string PostContent { set;get;}
}
Run Code Online (Sandbox Code Playgroud)

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

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

现在只需确保使用此属性构建要与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>
}
Run Code Online (Sandbox Code Playgroud)


Kld*_*Kld 4

[ValidateInput(false)]在您希望允许 HTML 的控制器中添加操作 (post) 属性:

[ValidateInput(false)]
[HttpPost]
public ActionResult PostForm(Model model)
{
 //
}
Run Code Online (Sandbox Code Playgroud)