Umbraco:根据节点ID获取根节点ID

时间:2016-09-21 14:53:35

标签: c# .net asp.net-mvc umbraco umbraco7

我是Umbraco的新手,我有5个根节点的问题,而且我有一个包含在这些根节点中的随机页面列表。我从这些页面接收的数据是NodeId,NodeName和Level。我尝试做的是获取每个页面的根节点信息。不幸的是,这是我遇到问题的地方,有没有办法根据NodeId获取根节点或1级节点的信息。

这是我到目前为止所得到的:

foreach (var item in pages)
{
    int level = item["level"].AsInt();
    if (level > 1){
        var currentItem = library.GetCurrentDomains(item.Id);
    }
}

我已尝试过library.GetCurrentDomains(item.Id),但这不起作用。

2 个答案:

答案 0 :(得分:1)

不完全确定这是否是您所需要的,也不是最佳方式,但您可以做类似的事情

# create the class view, named as you need
class UserFormView(View):
    # define the form to use, in this case the form we created
    form_class = UserForm
    # define the template_name, your main html file wher your are goin to use the form
    template_name = 'usersControll/add.html'
    # and the reverse_lazy is helpfull when the user succesfully added a new user
    # replace 'users-add' with the name of your rute 
    success_url = reverse_lazy('users-add')

    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form, request)

    def form_valid(self, form):
        # when the info the user gave us is valid, stop the commit 
        # so we can give some nice format to this info
        user = form.save(commit=False)
        # the "form.cleaned_data" help us to give a standar format to the info
        username = form.cleaned_data['username']
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        password = 'tempPass'
        user.set_password(password)
        # aaand we save it to the database
        user.save()

        # in my case a send a succesfull massage to the user indicating that
        # went fine
        messages.add_message(self.request, messages.SUCCESS, "El usuario <b>form.cleaned_data['first_name']</b> fue registrado exitosamente.")
        return super(UserFormView, self).form_valid(form)

    def form_invalid(self, form, request):
        return render(request, self.template_name, {'form': form})

获得第二级&#34; root&#34;任何节点。我想; - )

答案 1 :(得分:1)

假设随机页面列表都是IPublishedContent,您可以在页面上使用扩展方法AncestorOrSelf(1),它将获取页面的根节点。 E.g。

foreach (var item in pages)
{
    var rootPage = item.AncestorOrSelf(1);

    //do something with the root node here
}