本文介绍了在Razor Helper中使用@section的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将版面的部分设置为必填项,但可根据各个页面进行配置.目前,我们通过一个小节来完成此操作.

We are trying to setup Sections of our layout to be Required but configurable based on the individual page. At the moment we do this with a Section.

@section FloatingNav {
    <h1>@Model.Name <span class="release-year">@Model.AverageRating</span></h1>
    <ul class="sub-nav">
        <li class="active"><a href="#episodes">Episodes</a></li>
        <li><a href="#episodes">Cast</a></li>
        <li>Reviews</li>
        <li>Related</li>
    </ul>
}

这要求您在每个新页面中都设置此块,但我想通过一些默认值和使用部分视图进行配置的选项来使此过程更容易.我希望设置像这样的Razor助手.

This requires you to setup this block in every new page but i wanted to make this process easier with some defaults and options to configure using a partial view. I was hoping to setup a Razor helper such as this.

@using System.Web.Mvc.Html
@helper FloatingNav(string name, int rating) {
    @section FloatingNav {
        <h1>
            name <span class="release-year">rating</span></h1>
        <ul class="sub-nav">
            <li class="active"><a href="#episodes">Episodes</a></li>
            <li><a href="#episodes">Cast</a></li>
            <li>Reviews</li>
            <li>Related</li>
        </ul>
    }
}
@helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName) {
    @section FloatingNav {
        @html.Partial(viewName)
    }
}
@helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName, object model) {
    @section FloatingNav {
        @html.Partial(viewName, model)
    }
}

因此要实现的语法类似于

So the syntax to implement would be something like

@Layout.FloatingNav(@Model.Name, @Model.AverageRating)

@Layout.FloatingNav("_SimpleNav", @Model)

问题是,剃刀助手似乎不了解该节的语法.有没有办法在Razor Helpers中包含部分?

The issue though is that it seems the Razor Helpers do not understand the section syntax. Is there a way to include sections in Razor Helpers?

推荐答案

我认为这不可能.

@helper@section语法是用于编译页面的特殊指令.

The @helper and @section syntax are special directives for compiling pages.

HelperResult(助手)不知道如何定义节.

A HelperResult (a helper) doesn't know how to define a section.

DefineSection 方法属于WebPageBase.

您可能不得不从另一个方向来解决这个问题.使用局部视图而不是辅助视图可能会解决此问题.

You might have to come at this from a different direction. Using partial views instead of helpers would probably fix this problem.

这篇关于在Razor Helper中使用@section的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:18