来自代码隐藏的 umbraco 数据类型和文本值

发布于 2024-10-05 12:56:46 字数 246 浏览 4 评论 0原文

我正在尝试从 API 获取下拉列表的文本值,但我真的很挣扎。

这就是我现在所拥有的:

Document doc = new Document(Node.GetCurrent().Id);

doc.GetProperty("fieldPropertyName").Value;

这将返回预值 id 的字符串表示形式。

我想要的是该预值的文本。

预先感谢您的帮助。

I am trying to get the text value of a dropdown list from the API and I am seriously struggling.

This is what I have at the moment:

Document doc = new Document(Node.GetCurrent().Id);

doc.GetProperty("fieldPropertyName").Value;

This returns a string representation of the id for the prevalue.

What I want is the text for that pre value.

Thanks in advance for the help.

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

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

发布评论

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

评论(3

傾城如夢未必闌珊 2024-10-12 12:56:46

使用库函数...

var stringValue = umbraco.library.GetPreValueAsString(Convert.ToInt32(doc.GetProperty("fieldName").Value));

Use the library function...

var stringValue = umbraco.library.GetPreValueAsString(Convert.ToInt32(doc.GetProperty("fieldName").Value));
眼藏柔 2024-10-12 12:56:46

请原谅这是在 VB 中。

这是我碰巧正在开发的语言。(我多么希望我可以使用 C#)

Imports System.Runtime.CompilerServices
Imports umbraco.cms.businesslogic.web
Imports umbraco.cms.businesslogic.datatype

Module UmbracoExtensionHelper


    <Extension()>
    Public Function GetCustomPropertyValueFromPreValues(ByVal doc As Document, ByVal propertyName As String)
        Dim returnValue As String = ""
        Dim objProperty As umbraco.cms.businesslogic.property.Property = doc.getProperty(propertyName)

        If objProperty IsNot Nothing Then
            Dim objPreValues = PreValues.GetPreValues(objProperty.PropertyType.DataTypeDefinition.Id)
            If objPreValues IsNot Nothing Then

                ''run through the ids of the datatypes and the value of the property
                For Each entry As DictionaryEntry In objPreValues
                    Dim currentPreValue As PreValue = CType(entry.Value, PreValue)
                    If currentPreValue.Id.ToString().ToLower() = objProperty.Value.ToString().ToLower() Then
                        returnValue = currentPreValue.Value.ToLower()
                        Exit For
                    End If
                Next

            End If
        End If

        Return returnValue
    End Function




End Module

Please excuse this being in VB.

This is the language I happened to be developing in. (How I wish I could use C#)

Imports System.Runtime.CompilerServices
Imports umbraco.cms.businesslogic.web
Imports umbraco.cms.businesslogic.datatype

Module UmbracoExtensionHelper


    <Extension()>
    Public Function GetCustomPropertyValueFromPreValues(ByVal doc As Document, ByVal propertyName As String)
        Dim returnValue As String = ""
        Dim objProperty As umbraco.cms.businesslogic.property.Property = doc.getProperty(propertyName)

        If objProperty IsNot Nothing Then
            Dim objPreValues = PreValues.GetPreValues(objProperty.PropertyType.DataTypeDefinition.Id)
            If objPreValues IsNot Nothing Then

                ''run through the ids of the datatypes and the value of the property
                For Each entry As DictionaryEntry In objPreValues
                    Dim currentPreValue As PreValue = CType(entry.Value, PreValue)
                    If currentPreValue.Id.ToString().ToLower() = objProperty.Value.ToString().ToLower() Then
                        returnValue = currentPreValue.Value.ToLower()
                        Exit For
                    End If
                Next

            End If
        End If

        Return returnValue
    End Function




End Module
如若梦似彩虹 2024-10-12 12:56:46

使用下面的代码

aspx 页面

 <asp:DropDownList ID="ddlLocation" ClientIDMode="Static" runat="server" AutoPostBack="true" CssClass="selectbox" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" />

这里的代码

  var regionItems = regionFolder.Children;
            if (regionItems.Count > 0) {
                foreach (Node region in regionItems) {
                    if (region.GetProperty(FieldName.REGIONNAME) != null && !string.IsNullOrEmpty(region.GetProperty(FieldName.REGIONNAME).Value)) {
                        ddlLocation.Items.Add(new ListItem(region.GetProperty(FieldName.REGIONNAME).Value, region.Id.ToString()));
                    }
                }
            }
            //ddlLocation.Items.Insert(0, "Choose");
            ddlLocation.Items.Insert(0, new ListItem("Choose", "0"));

REGIONNAME= 我们的字段名称,

use following code

aspx page

 <asp:DropDownList ID="ddlLocation" ClientIDMode="Static" runat="server" AutoPostBack="true" CssClass="selectbox" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" />

Code Behind

  var regionItems = regionFolder.Children;
            if (regionItems.Count > 0) {
                foreach (Node region in regionItems) {
                    if (region.GetProperty(FieldName.REGIONNAME) != null && !string.IsNullOrEmpty(region.GetProperty(FieldName.REGIONNAME).Value)) {
                        ddlLocation.Items.Add(new ListItem(region.GetProperty(FieldName.REGIONNAME).Value, region.Id.ToString()));
                    }
                }
            }
            //ddlLocation.Items.Insert(0, "Choose");
            ddlLocation.Items.Insert(0, new ListItem("Choose", "0"));

here REGIONNAME= our field name,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文