简体   繁体   English

如何通过删除MVC EditorFor Form上的HTML标记来替换textarea中的文本

[英]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.

相关问题 如何在 mvc 4 razor 中的 Html.EditorFor 中呈现 HTML 标签 - How to render HTML tags in Html.EditorFor in mvc 4 razor C#MVC表单:如何利用html.editorfor的优势为简单的反馈表单构建视图? - c# mvc form: how to build view for simple feedback form leveraging the benefit of using html.editorfor? 如何在ASP.net MVC 5中使用@ Html.EditorFor自动填充表单中的字段? - How to auto fill fields in a form using @Html.EditorFor in ASP.net MVC 5? 如何在不删除 html 标签的情况下将 html 转换为文本 - How to convert html to text without removing html tags 在MVC 1中替代Html.EditorFor - Alternative to Html.EditorFor in MVC 1 MVC4-如何将EditorFor()与数据库中的动态表单数据一起使用 - MVC4 - how to use EditorFor() with dynamic form data from a database 如何使 html.editor 在 asp.net mvc 2 中不可见? - how to make html.editorfor unvisible in asp.net mvc 2? ASP .NET MVC如何使用@ Html.EditorFor增加输入字段的宽度 - ASP .NET MVC How to increase the width of an inputfield with @Html.EditorFor 如何使用EditorFor在MVC 5中添加动态HTML属性? - How Can I Add Dynamic HTML Attributes in MVC 5 using EditorFor? 如何用 HTML 替换文本替换定制代码标记/标签? - How to replace bespoke code markup/tags with HTML replacement text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM