how to use allowhtml attribute for an action in mvc5

被刻印的时光 ゝ 提交于 2019-12-30 10:14:24

问题


I am developing an mvc 5 project and I want to use ckEditor for input data so in this editor I saved data
after I could insert data but when dispalying it has an error See Imaage


回答1:


You can apply AllowHtml attribute to the property which holds the markup in your view model class.

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

And use this view model in your HttpPost action method and everything will work fine.

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

Now just make sure you are using this property to build the text area to be used with 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>
}



回答2:


Add the [ValidateInput(false)] attribute the action (post) in the controller that you want to allow HTML for:

[ValidateInput(false)]
[HttpPost]
public ActionResult PostForm(Model model)
{
 //
}



回答3:


In order to use the [ValidateInput(false)] attribute, first you have to add the attribute requestValidationMode="2.0" in your httpRuntime tag in the site's Web.config:

<system.web>
    <httpRuntime targetFramework="4.5.1" requestValidationMode="2.0" />
    ...
</system.web>

That worked for me.



来源:https://stackoverflow.com/questions/37886474/how-to-use-allowhtml-attribute-for-an-action-in-mvc5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!