MVC3 在 ViewBag 文本中添加换行符
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您放入视图中时,使用
代替
That 将向编译器表明字符串是 html 并且不需要这样编码。
When you throw into your view, use
instead of
That will signify to the compiler that string is html and does not need to be encoded as such.
使用
string[]
来保存错误。这样它们就是一组格式良好且不同的错误,而不仅仅是一个长字符串。在您的控制器中,初始化
ViewBag
属性:在您的视图中,显示以下错误:
关注点分离
您不应该在控制器中处理标记布局(即换行符、或任何其他 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:In your View, displaying these errors:
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[]
.这对我有用:
控制器:
视图:
This worked for me :
Controller:
View:
我的首选方法是简单地将
添加到控制器代码中的行时间作为 ViewData["msg"] ,然后可以从剃刀页面中提取,如下所示。控制器代码:
Razor 表单代码:
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:
Razor Form Code: