在 ViewData[“msg”] 内传递 NewLine(或

发布于 2024-10-04 11:14:53 字数 151 浏览 6 评论 0原文

如何在 ViewData["msg"] 内传递 Environment.NewLine (或
) 在视图本身内呈现为真实的

我正在使用 C#...

How do I pass an Environment.NewLine (or a < br />) inside a ViewData["msg"]
that renders as a real <br /> inside the View itself?

I'm using C# ...

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

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

发布评论

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

评论(3

瞎闹 2024-10-11 11:14:53

您只需使用

HttpUtility.HtmlEncode("<br />");

查看此处 了解更多详情。

编辑

很抱歉,我一开始并没有理解这个问题。我以为你想对传递的字符串进行编码。有时我读问题的速度太快了:)

无论如何,要完全回答你的问题,请看看这些示例。假设在你的行动中你有:

[HttpGet]
public virtual ActionResult Index() {
    ViewData["Message"] = "This is a test." + "<br />" + "Does it work?";
    return View();
}

并且在你看来你已经

<p>Sample 1: <br /><%= ViewData["Message"]%></p>
<p>Sample 2: <br /><%: ViewData["Message"]%></p>

注意到这两个结构之间的区别。您将得到以下结果:

Sample 1: 
This is a test.
Does it work?

Sample 2: 
This is a test.<br />Does it work?

所以第一个示例用法回答了您的问题。关键是不要对字符串进行编码,这与我一开始所理解的相反。第二个构造是使用 <%: %> 的构造,如果您使用 .NET 4.0,则可用,自动对其呈现的每个字符串进行编码。

如果您仍然想使用这个最新的构造并避免对字符串进行编码,您必须使用一个技巧。只需告诉构造该字符串已经编码,不需要重新编码。要实现此目的,请使用此示例代码

[HttpGet]
public virtual ActionResult Index() {
    ViewData["Message2"] = MvcHtmlString.Create( "This is a test." + "<br />" + "Does it work?" );
    return View();
}

,并在视图中通常使用 <%: %> 构造

<p>Sample 3: <br /><%: ViewData["Message2"]%></p>

有关使用 MvcHtmlString 及其工作原理的更多详细信息,请参阅此 问题

再见!

You can simply use

HttpUtility.HtmlEncode("<br />");

Have a look here for further details.

EDIT:

I am sorry but I did not understand the question at the beginning. I thought you wanted to encode the passed string. Sometimes I read question too fast :)

Anyway to completely answer your question have a look at these samples. Suppose in your action you have:

[HttpGet]
public virtual ActionResult Index() {
    ViewData["Message"] = "This is a test." + "<br />" + "Does it work?";
    return View();
}

And in your view you have

<p>Sample 1: <br /><%= ViewData["Message"]%></p>
<p>Sample 2: <br /><%: ViewData["Message"]%></p>

Notice the difference between the two construct. You will get the following results:

Sample 1: 
This is a test.
Does it work?

Sample 2: 
This is a test.<br />Does it work?

So the first sample usage answer your question. The key is to simply not encoding the string as opposed to what I understood at the beginning. The second construct, the one that use <%: %>, available if you're using .NET 4.0, automatically encode every string it render.

If you still want to use this latest construct and avoid that the string would be encoded you have to use a trick. Simply tell the construct that the string is already encoded and it does not need to be re-encoded. To achieve this use this sample code

[HttpGet]
public virtual ActionResult Index() {
    ViewData["Message2"] = MvcHtmlString.Create( "This is a test." + "<br />" + "Does it work?" );
    return View();
}

and in the view normally use the <%: %> construct

<p>Sample 3: <br /><%: ViewData["Message2"]%></p>

For more details on using the MvcHtmlString and how it works please refer to this question on SO.

Ciao!

忘你却要生生世世 2024-10-11 11:14:53

您是否在 ViewData 字符串本身中尝试了 \n 或 \r ?

Did you try \n or \r inside the ViewData string itself?

眼眸印温柔 2024-10-11 11:14:53

使用这个:

TempData["MsgString"] = MvcHtmlString.Create("No Data" + "<br />" + "For Something");

或者,使用变量:

TempData["MsgString"] = MvcHtmlString.Create(Strings.StrNoData + "<br />" + Strings.StrSomething);

Use this:

TempData["MsgString"] = MvcHtmlString.Create("No Data" + "<br />" + "For Something");

Or, with variables:

TempData["MsgString"] = MvcHtmlString.Create(Strings.StrNoData + "<br />" + Strings.StrSomething);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文