英文:
Getting value from rich text element in Contentful?
问题
-
我需要循环整个文档主体并分离标题-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.
-
I need to loop the entire Document body and isolate the Heading-2 portions and then render the values into a list
-
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论