Sitecore MVC可编辑链接在页面编辑器中

时间:2015-09-01 18:19:35

标签: asp.net-mvc sitecore sitecore8

我尝试从自定义模型输出链接和链接文本,以便在页面编辑器中进行编辑。它工作正常但不在页面编辑器中。 在我的模型类中,我有两个这样的字符串:

public string Link { get; set; }
public string LinkText { get; set; }

我尝试在这样的存储库中编辑文本:

model.LinkText = FieldRenderer.Render(item, "Link Text");
model.Link = LinkManager.getItemUrl(item);

在我的.cshtml文件中,我输出的字段如下:

<a href="@Model.Link>@Model.LinkText<a/>

我是以错误的方式解决这个问题,还是可以通过一些调整来使用此代码?

3 个答案:

答案 0 :(得分:3)

要让编辑器在页面编辑器中工作,您需要更改属性的类型。

更改

public string Link { get; set; }

public IHtmlString Link { get; set; }

这将允许MVC呈现页面编辑器的html组件。这应该在页面编辑器中处理你的链接文本。

使链接可编辑取决于字段类型。

答案 1 :(得分:1)

This depends on the field type you have on your template.

If you are using a general link field You would need to use a single property in your model for the link (of type HtmlString). The way you are initialising your 'LinkText' property is fine. In your razor code you would just use @Model.PropertyName.

If you are using two separate fields for the target page and the link text, you would instead need to use edit frames (not sure of the mvc equivalent for this) or a custom page editor button to open a popup where the field can be edited content editor style.

答案 2 :(得分:0)

尝试像这样的东西,而Link字段应该是General Link。 :

@using (BeginRenderLink(x => x.Link, isEditable: true))
{
    @Editable(x => x.Title);
} 
相关问题