You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在ASP.NET MVC中使用晚绑定检索选项集文本

解决ASP.NET MVC中Dynamics CRM选项集文本值显示问题

要获取选项集对应的文本值,你可以利用Dynamics CRM Entity对象的FormattedValues集合,它直接存储了字段的格式化显示文本(包括选项集文本)。以下是具体修改步骤:

1. 更新实体模型

在AccountEntityModels中新增属性用于存储选项集文本:

Public Property LicenceStatus As OptionSetValue
Public Property LicenceStatusValue As String
Public Property LicenceStatusText As String ' 新增属性存储选项集文本

2. 修改数据获取逻辑

在RetriveRecords方法中,除了获取选项集数值,同时从FormattedValues中提取对应文本:

Public Shared Function RetriveRecords() As List(Of AccountEntityModels)
    Using service As OrganizationService = New OrganizationService("MyConnectionString")
        Dim query As QueryExpression = New QueryExpression With {
            .EntityName = "account",
            .ColumnSet = New ColumnSet("new_licencestatus")
        }
        Dim info As List(Of AccountEntityModels) = New List(Of AccountEntityModels)()
        Dim accountRecord As EntityCollection = service.RetrieveMultiple(query)

        If accountRecord IsNot Nothing AndAlso accountRecord.Entities.Count > 0 Then
            Dim accountModel As AccountEntityModels

            For i As Integer = 0 To accountRecord.Entities.Count - 1
                accountModel = New AccountEntityModels()
                Dim entity = accountRecord(i)
                If entity.Contains("new_licencestatus") AndAlso entity("new_licencestatus") IsNot Nothing Then
                    ' 获取选项集数值
                    accountModel.LicenceStatusValue = (CType(entity("new_licencestatus"), OptionSetValue)).Value.ToString
                    ' 获取选项集文本
                    If entity.FormattedValues.ContainsKey("new_licencestatus") Then
                        accountModel.LicenceStatusText = entity.FormattedValues("new_licencestatus")
                    End If
                End If
                info.Add(accountModel)
            Next
        End If
        Return info
    End Using
End Function

3. 在视图中展示文本值

修改MVC视图,直接使用新增的LicenceStatusText属性显示文本:

@For Each item In Model
    @<p>许可证状态:@item.LicenceStatusText</p>
Next

备选方案:通过元数据获取选项集全量键值对

如果需要预先获取该选项集的所有选项(比如用于生成下拉列表),可以通过RetrieveAttributeRequest获取元数据:

Public Shared Function GetLicenceStatusOptions() As Dictionary(Of Integer, String)
    Using service As OrganizationService = New OrganizationService("MyConnectionString")
        Dim request As RetrieveAttributeRequest = New RetrieveAttributeRequest With {
            .EntityLogicalName = "account",
            .LogicalName = "new_licencestatus",
            .RetrieveAsIfPublished = True
        }
        Dim response As RetrieveAttributeResponse = CType(service.Execute(request), RetrieveAttributeResponse)
        Dim picklistAttributeMetadata As PicklistAttributeMetadata = CType(response.AttributeMetadata, PicklistAttributeMetadata)
        Dim options As OptionMetadataCollection = picklistAttributeMetadata.OptionSet.Options

        Dim optionDict As New Dictionary(Of Integer, String)()
        For Each option In options
            optionDict.Add(option.Value.Value, option.Label.UserLocalizedLabel.Label)
        Next
        Return optionDict
    End Using
End Function

使用时可先获取该字典,再根据数值匹配对应文本。

内容的提问来源于stack exchange,提问作者PeteN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:01:10