使用以下内容:
- MVC 3
- VB.NET
- RAZOR VIEW 引擎
- Html 助手类
在我的应用程序中,我有一个发送到视图并显示的列表(字符串)。我正在尝试以不超过 8 列宽到多行长的表格格式进行显示。我已经编写了一个自定义帮助器类来处理这个问题,但是视图显示了带有电子邮件地址的原始 html 代码,但它没有呈现为带有行等的表格。任何解决此问题的想法。下面是帮助器类,模板,最后是视图。
Imports System.Runtime.CompilerServices
Public Module TableHelper
<Extension()> _
Public Function CreateEmailTable(ByVal helper As HtmlHelper, ByVal emaillist As List(Of String)) As MvcHtmlString
Dim htmlDisplayer As String = Table()
Dim counter As Integer = 0
For Each item In emaillist
If counter = 0 Then
htmlDisplayer = htmlDisplayer + NRow()
End If
counter += 1
If Not counter >= 8 Then
htmlDisplayer = htmlDisplayer + Ntd(item)
Else
counter = 0
htmlDisplayer = htmlDisplayer + CRow()
End If
Next
htmlDisplayer = htmlDisplayer + CTable()
Dim x As MvcHtmlString = MvcHtmlString.Create(htmlDisplayer)
Return x
End Function
Public Function Table() As String
Return String.Format("<table>")
End Function
Public Function CTable() As String
Return String.Format("</table>")
End Function
Public Function NRow() As String
Return String.Format("<tr>")
End Function
Public Function CRow() As String
Return String.Format("</tr>")
End Function
Public Function Ntd(ByVal text As String) As String
Return String.Format("<td>{0}</td>", text)
End Function
End Module
模板视图是:
@ModelTYPE List(Of String)
@Html.CreateEmailTable(Model)
最后的观点是:
Modeltype xxxxxxxxx.EmailsVM
@Code
ViewData("Title") = "Mass Emails Sent"
End Code
@Using Html.BeginForm
@<fieldset>
<p>
@Html.Partial("_EmailsVM", Model.failEmList)
</p>
</fieldset>
End Using
这是我在浏览器中查看时显示的视图页面源代码的片段:
<table><tr><td>xxxxxxxxx@yahoo.com; </td><td>xxxxxxxx@xxxxxxx.net; </td><td>xxxxx.xxxxxxxx@xxxxx; </td><td>
可以看出,它的 html 标记正在失去浏览器接受的格式......