我在将html从控制器传递到视图时出现问题。如果我将html粘贴在ViewBag中然后用@Html.Raw()
显示它,它就可以工作。当我将html粘贴到模型(属性类型字符串)中,将模型放入视图模型然后使用@Html.Raw()
来显示html(项目)时,就会出现问题。而不是显示html它给了我这个:
VB $ AnonymousDelegate_0`2 [System.Object,System.String]
我无法将其留在ViewBag中,因为html需要根据模型中的数据进行更改。
根据要求我添加了代码(我简化了一下)。除视图中的@Html.Raw(Function(modelItem) item.eventicon)
外,所有工作均有效。
谢谢!
Public Class activitypanel
Public Property eventwhat As String
Get
Return m_eventwhat
End Get
Set(value As String)
m_eventwhat = value
End Set
End Property
Private m_eventwhat As String
Public Property eventwhen As DateTime
Get
Return m_eventwhen
End Get
Set(value As DateTime)
m_eventwhen = value
End Set
End Property
Private m_eventwhen As DateTime
Public Property eventwho As String
Get
Return m_eventwho
End Get
Set(value As String)
m_eventwho = value
End Set
End Property
Private m_eventwho As String
<AllowHtml>
Public Property eventicon As String
Get
Return m_eventicon
End Get
Set(value As String)
m_eventicon = value
End Set
End Property
Private m_eventicon As String
Public Property eventlink As String
Get
Return m_eventlink
End Get
Set(value As String)
m_eventlink = value
End Set
End Property
Private m_eventlink As String
End Class
Public Class upcomingpanel
Public approvals As Integer
Public tickets As Integer
Public defects As Integer
Public tasks As Integer
End Class
Public Class welcomeViewModel
Public activity As IList(Of activitypanel)
Public Property upcoming As IList(Of upcomingpanel)
End Class
在Controller Action中:
Dim actvities As New List(Of activitypanel)
Loop through data....
Dim itemtype As Integer = drv("type")
Dim actitem As New activitypanel
If itemtype = 0 Then
actitem.eventicon = "<i class='fa fa-check fa-fw'></i>"
else
...
end
actvities.Add(actitem)
end loop...
Dim vm As New welcomeViewModel()
vm.upcoming = upcoming
vm.activity = actvities
Return View(vm)
在视图中:
<table class="table table-hover ">
<tbody>
@For Each item In Model.activity
@<tr>
<td style=" text-wrap:none;width:20%;">
@Html.Raw(Function(modelItem) item.eventicon)
@Html.DisplayFor(Function(modelItem) item.eventwho)
@Html.DisplayFor(Function(modelItem) item.eventwhat)
</td>
</tr>
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) item.eventwhen)
</td>
</tr>
Next
</tbody>
答案 0 :(得分:0)
行。我有一个解决方案(针对遇到此问题的其他任何人)。
在模型定义中,将以下内容添加到包含html的属性:
<AllowHtml> <DataType(DataType.Html)>
Public Property eventicon As String
Get
Return m_eventicon
End Get
Set(value As String)
m_eventicon = value
End Set
End Property
在视图中使用@html.displayfor
@Html.DisplayFor(Function(modelItem) item.eventicon)
就是这样。感谢您的反馈和建议。