【问题标题】:How to display plain text instead of with HTMLText in textarea in ASP.Net Core如何在 ASP.Net Core 的 textarea 中显示纯文本而不是 HTMLText
【发布时间】:2021-09-06 17:32:10
【问题描述】:

如何在 ASP.Net Core 的 textarea 中显示纯文本而不是 HTMLText

这是我的看法

<div class="form-group row">
                <label asp-for="Decription" class="col-sm-3 col-form-label">Description</label>
                <div class="col-sm-7">
                    <textarea type="text" asp-for="Decription" class="form-control"></textarea>
                </div>
            </div>

这是我的观点

如何在这个 textarea 中显示纯文本而不是 HtmlText。

【问题讨论】:

  • 嗨@Abhay,您的代码可以正常工作,但&lt;textarea&gt; 元素不能包含格式化的HTML。根据MDN,“字符数据”是&lt;textarea&gt; 唯一允许的内容。如果您需要像 textarea 这样允许格式化 HTML 的东西,您正在寻找一个所见即所得的编辑器,如 CKEditor、TinyMCE 或 Kendo 的编辑器。
  • @Html.Raw("你的 html 文本")
  • 查看Link
  • 我已经使用了 CKEditor,但这仅用于管理员,这是客户端,我的客户端要求不显示编辑器,只显示 textarea 和那些数据。

标签: asp.net asp.net-core asp.net-core-mvc


【解决方案1】:

如果您想显示不带任何格式的内容,可以使用Regex.Replace(input, "&lt;.*?&gt;", String.Empty) 去除字符串中的所有 Html 标签。

您可以在后端进行更改:

var model = new TestModel() { 
       Decription="<p><strong>Test</strong> is a Special Item in our <i>Restraunt</i>.</p>" 
};
model.Decription = Regex.Replace(model.Decription, "<.*?>", String.Empty);

或者改变前端:

@model TestModel

@using System.Text.RegularExpressions;

<textarea type="text" name="Decription" class="form-control">@Regex.Replace(Model.Decription, "<.*?>", String.Empty)</textarea>

【讨论】:

  • 感谢您的帮助,如果我们仅更改视图,那么它也可以。不需要在后端控制器端使用 Regex.Replace(model.Decription, "<.>", String.Empty); 如果我们只使用前端那么也可以工作。
猜你喜欢
  • 2012-08-09
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 2013-01-04
  • 2015-10-20
相关资源
最近更新 更多