【问题标题】:Can I use a TextAreaFor helper as an html editor?我可以使用 TextAreaFor 助手作为 html 编辑器吗?
【发布时间】:2013-07-07 04:03:32
【问题描述】:

我可以使用 TextAreaFor 帮助器将 html 保存到数据库,然后将其加载到呈现它的页面上吗?

【问题讨论】:

    标签: asp.net-mvc-4 razor-2 sql-server-2012-express


    【解决方案1】:

    当然可以,但是默认情况下,ASP.NET 的请求过滤器不允许传入的请求包含 html、js 等。因此,您需要为模型的目标属性禁用此选项。最好的方法是用AllowHtmlAttribute 标记属性。

    public class YourViewModel
    {
            [AllowHtml]
            public string description { get; set; }
    }  
    

    并在视图中渲染

    @Html.Raw(Model.description)
    

    或者,您可以禁用对操作的验证请求,例如

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult YourAction(YourViewModel model)
    {
    
    }
    

    但这不是最好的解决方案。

    【讨论】:

    • 请注意,您不能将 AllowHtmlAttribute 应用于控制器操作,因为它只能应用于属性。 (See the MSDN entry.)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2016-10-09
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多