[英]How to replace the text in a textarea by removing HTML tags on MVC EditorFor Form
I have a field in my database called emailhtml where I save a plain text email string from a textarea called plaintext. 我的数据库中有一个名为emailhtml的字段,我在一个名为plaintext的textarea中保存一个纯文本电子邮件字符串。 I save the \\n etc by using:
我使用以下方法保存\\ n等:
string test = plaintext.Replace(System.Environment.NewLine, "<br />");
Now in my EditorFor form I want to be able to display the field with the include line breaks etc but I cannot seem to work out how to do so. 现在在我的EditorFor表单中,我希望能够显示包含换行符等字段,但我似乎无法弄清楚如何这样做。 I have tried:
我努力了:
@Html.TextAreaFor(c => c.Campaign.OwnHtml.Replace("<br />",System.Environment.NewLine ), new {rows="20", cols="20"})
But this results in an InvalidOperationException: 但是这会导致InvalidOperationException:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. 模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。
You can use as shown below as well : 您也可以使用如下所示:
@{
var text = Model.Campaign.OwnHtml.Replace("<br />", System.Environment.NewLine);
}
@Html.TextArea("textArea", text, new { rows = "20", cols = "20" })
It looks like the problem is that you aren't allowed to call .Replace() inside your lambda expression there. 看起来问题是你不允许在你的lambda表达式中调用.Replace()。 You need to bind directly to a property.
您需要直接绑定到属性。
Try adding another property to whatever class your 'c' corresponds to. 尝试将另一个属性添加到“c”对应的任何类别。 For example:
例如:
public string OwnHtmlWithLineBreaks
{
get
{
return OwnHtml.Replace("<br />", System.Environment.NewLine);
}
}
Then in your view, change your TextAreaFor line to the following, binding to the new computed property: 然后在您的视图中,将TextAreaFor行更改为以下内容,绑定到新的计算属性:
@Html.TextAreaFor(c => c.Campaign.OwnHtmlWithLineBreaks, new { rows = "20", cols = "20" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.