[C#]自定义 Razor 视图的基类

先上代码

需要继承WebViewPage 或 WebViewPage

using System.Web.Mvc;

namespace Library
{
    public abstract class Library : WebViewPage
    {
        public HtmlHelper Fish { get; set; }
        public override void InitHelpers()
        {
            base.InitHelpers();
            Fish = new HtmlHelper(ViewContext, this);
        }
    }
    public abstract class Library : WebViewPage
    {
        public HtmlHelper Fish { get; set; }
        private ViewDataDictionary _viewData;

        public override void InitHelpers()
        {
            base.InitHelpers();
            Fish = new HtmlHelper(ViewContext, this);
        }
        protected override void SetViewData(ViewDataDictionary viewData)
        {
            _viewData = new ViewDataDictionary(viewData);
            base.SetViewData(_viewData);
        }
        public new TModel Model
        {
            get
            {
                return ViewData.Model;
            }
        }
        public new ViewDataDictionary ViewData
        {
            get
            {
                if (_viewData == null)
                {
                    SetViewData(new ViewDataDictionary());
                }
                return _viewData;
            }
            set
            {
                SetViewData(value);
            }
        }
    }
}

mvc页面使用方法

@inherits Library.Library


@Fish.TextBox("text")
 

                            
                        
                    
                    
                    

你可能感兴趣的:([C#]自定义 Razor 视图的基类)