替换文本区域中的换行标签? [英] Replace the line break tag in a textarea?

查看:62
本文介绍了替换文本区域中的换行标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在详细信息字段中,字符串可以包含换行符.例如:正在测试
更多测试".

In the field details the string can contain line break tags. Example: "testing
more testing ".

如何在视图中将换行标记替换为文本区域中的换行符?

How in the view can I replace the line break tag with a line break in the textarea?

错误:

模板只能与字段访问,属性访问,一维数组索引或单参数自定义索引器表达式一起使用.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

@Html.TextAreaFor(model => model.details.ToString().Replace("<br>", "\n"), new { @class = "form-control"})

推荐答案

我将建议这三种方式

首先,使用 @ Html.TextArea

 @Html.TextArea("details", Model.details.ToString().Replace("<br>", "\n"), new { @class = "form-control"})

第二,不是标签的一部分,但非常有用,它是在页面加载时使用Javascript,我的示例使用了JQuery

Second, not part of your tag but very useful is using Javascript at page load, my example uses JQuery

$(document).ready(function() {
    var re = "@Html.Raw(Model.details)";
    $("#details").val(re.replace("<br>", "\n"));
});

第三,要使用 @ Html.TextAreaFor 使用,需要在绑定之前替换文本,例如:

Third, To use using @Html.TextAreaFor, you need to replace the text before binding like:

@{
    ViewBag.Title = "Home Page";
    Model.details = Model.details.Replace("<br>", "\n");
}

@Html.TextAreaFor(model => model.details, new { @class = "form-control"})

这篇关于替换文本区域中的换行标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆