Access global page variable in helper

后端 未结 3 1911
执念已碎
执念已碎 2021-02-06 20:47
@{
    int i = 0;
}

@helper Text() {
    
}

i is not accessible in helper. How to acce

3条回答
  •  面向向阳花
    2021-02-06 21:31

    You can achieve this by changing base class for your view. This scenario applies to situation where helper is declared in view.

    Create a base class that inherits from WebViewPage and introduce shared field or property:

    public class MyBasePage : WebViewPage
    {
        public int i;
    
        public override void Execute()
        { }
    }
    

    Using @inherits directive change base class. And now field/property is acessible both from "page context" and helper:

    @inherits NamespaceOfYourBaseClass.MyBasePage
    @{
        i = 0;
    }
    
    @helper Text() {
        
    }
    

    If you want to have a thing that is close to term "page property/field" but dont want to create a base class or helpers are stored within App_Code folder then you can try WebPageBase.Page property.

    MSDN: Provides property-like access to page data that is shared between pages, layout pages, and partial pages.

    The code in this case would be:

    @{
        Page.i = 0;
    }
    
    @helper Text() {
        
    }
    

    The drawback is that Page property is of type dynamic and thus does not support intellisense. As an alternative to Page there is another property - WebPageBase.PageData.

    MSDN: Provides array-like access to page data that is shared between pages, layout pages, and partial pages.

    In this case a class-container of strings/ints keys for "page variables" could be created. And the code would be like:

    // class visible to views and helpers
    class MyViewFields {
      public const string i = "MyViewFields.i"; // or maybe generate guid for key so there would be not doubts about its uniqueness.. but how would you debug this? :)
    }
    
    // in MyView.cshtml
    @{
        PageData[MyViewFields.i] = 0
    }
    
    @helper Text() {
        
    }
    

    This at least provides constraints for shared page data but still no control over value type.

提交回复
热议问题