MVC3 在 ViewBag 文本中添加换行符

发布于 2025-01-03 12:45:07 字数 526 浏览 3 评论 0原文

我有一个 MVC3 C#.Net Web 应用程序。我正在循环访问数据表。有些行导入正常,有些则不行。我想以列表格式将错误列表发送回视图。我将以下文本分配给 ViewBag 属性,

Input error on Row(1) Cell(3) Input string was not in a correct format.<br/> Input error on Row(4) Cell(3) Input string was not in a correct format.<br/>

我希望 br 会在 HTML 中写入换行符。事实并非如此。我希望错误消息看起来像这样

Input error on Row(1) Cell(3) Input string was not in a correct format.
Input error on Row(4) Cell(3) Input string was not in a correct format.

有什么想法吗?

I have an MVC3 C#.Net web app. I am looping through a DataTable. Some rows are importing OK, some are not. I am wanting to send a list of errors back to the view in a list format. I am assigning the following text to a ViewBag property

Input error on Row(1) Cell(3) Input string was not in a correct format.<br/> Input error on Row(4) Cell(3) Input string was not in a correct format.<br/>

I was hoping the br would write a line break in the HTML. It does not. I want the error message to look like this

Input error on Row(1) Cell(3) Input string was not in a correct format.
Input error on Row(4) Cell(3) Input string was not in a correct format.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妞丶爷亲个 2025-01-10 12:45:07

当您放入视图中时,使用

@Html.Raw(ViewBag.Test)

代替

@ViewBag.Test

That 将向编译器表明字符串是 html 并且不需要这样编码。

When you throw into your view, use

@Html.Raw(ViewBag.Test)

instead of

@ViewBag.Test

That will signify to the compiler that string is html and does not need to be encoded as such.

怪我太投入 2025-01-10 12:45:07

使用 string[] 来保存错误。这样它们就是一组格式良好且不同的错误,而不仅仅是一个长字符串。

在您的控制器中,初始化 ViewBag 属性:

ViewBag.Errors = new string[] { "First error", "Second error" };

在您的视图中,显示以下错误:

@foreach (string error in ViewBag.Errors)
{
    @Html.Label(error)
    <br />
}

关注点分离

您不应该在控制器中处理标记布局(即换行符、或任何其他 DOM 元素)。 演示文稿应仅由视图处理。这就是为什么最好传递一个string[]

Use a string[] to hold your errors. That way they are a well-formed and distinct set of errors instead of just one long string.

In your Controller, initializing the ViewBag property:

ViewBag.Errors = new string[] { "First error", "Second error" };

In your View, displaying these errors:

@foreach (string error in ViewBag.Errors)
{
    @Html.Label(error)
    <br />
}

Separation Of Concerns

You shouldn't be handling markup layout within your Controller (i.e. line breaks, or any other DOM elements). The presentation should be handled solely by the View. Which is why it would be best to pass a string[].

乖不如嘢 2025-01-10 12:45:07

这对我有用:

控制器:

 ViewBag.Msg += Environment.NewLine + "xxxx";

视图:

<p class="@ViewBag.MsgColor">
        @Html.Raw(@ViewBag.Msg.Replace(Environment.NewLine, "<br/>"))
 </p>

This worked for me :

Controller:

 ViewBag.Msg += Environment.NewLine + "xxxx";

View:

<p class="@ViewBag.MsgColor">
        @Html.Raw(@ViewBag.Msg.Replace(Environment.NewLine, "<br/>"))
 </p>
超可爱的懒熊 2025-01-10 12:45:07

我的首选方法是简单地将
添加到控制器代码中的行时间作为 ViewData["msg"] ,然后可以从剃刀页面中提取,如下所示。

控制器代码:

ViewData["msg"] = "Your Query <br /> has been processed.";

Razor 表单代码:

@Html.Raw(@MsgFormatted)

My preferred way of doing this is to simply add a <br /> to the line time in the controller code as ViewData["msg"] which can then be pulled from the razor page as below.

Controller Code:

ViewData["msg"] = "Your Query <br /> has been processed.";

Razor Form Code:

@Html.Raw(@MsgFormatted)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文