从Contentful的富文本元素中获取数值?

huangapple go评论68阅读模式
英文:

Getting value from rich text element in Contentful?

问题

  1. 我需要循环整个文档主体并分离标题-2部分,然后将值呈现在列表中。

  2. 我需要获取文档类型中的第一个段落元素并呈现该值。

我正在使用以下代码:
Case 1:

foreach (IContent node in ccContent.Items.bodyText.Content)
{
   switch (node)
   {
      case Heading2 h:
--------> h.Content.Value可以在断点中看到,但不能在代码中呈现
        break;
      default:
        break;
   }
}

Case 2:

foreach (var article in articles)
{
    foreach (IContent node in article.response.Content)
    {
       switch (node)
       {
          case Paragraph p:
            IContent content = p.Content.FirstOrDefault();
--------> content.Value可以在断点中看到,但不能在代码中呈现吗?
            break;
       }
    }
}
英文:

I'm writing a Blazor site using Contentful as the CMS. I have 2 needs surrounding the Rich text Document type.

  1. I need to loop the entire Document body and isolate the Heading-2 portions and then render the values into a list

  2. I need to get the first Paragraph element in a document type and render the value of that.

I am using the following code:
Case 1:

foreach (IContent node in ccContent.Items.bodyText.Content)
{
   switch (node)
   {
      case Heading2 h:
-------->   h.Content.Value can be seen in a breakpoint, but not 
-------->   available to render in code
        break;
      default:
        break;
   }
}

Case 2:

foreach (var article in articles)
{
    foreach (IContent node in article.response.Content)
    {
       switch (node)
       {
          case Paragraph p:
            IContent content = p.Content.FirstOrDefault();
-------->   content.Value can be seen in a breakpoint, but not 
-------->   available to render in code?
            break;
       }
    }
}

答案1

得分: 0

你需要对IContent content进行类型检查并转换为正确的类型。

例如:

if(content is Text) {
   var v = (content as Text).Value;
}
英文:

You need to type check and cast your IContent content to the correct type.

For example:

if(content is Text) {
   var v = (content as Text).Value;
}

huangapple
  • 本文由 发表于 2023年3月3日 19:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定