改变对MVC4基本视图类型 [英] Changing base view type on MVC4

查看:167
本文介绍了改变对MVC4基本视图类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关从下面的链接MVC改变基本视图类型后:

I've read a post about changing base view type on MVC from the link below:

<一个href=\"http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx\">http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx

我也跟着指示,但我的网页仍 System.Web.Mvc.WebViewPage 继承。我无法达到我的自定义视图定义基地的财产,我得到的运行时错误。当我使用 @inherits 关键字,它修复。

I followed the instructions but my page still inherits from System.Web.Mvc.WebViewPage. I can't reach any property defined in my custom view base and I get an error on runtime. When I use @inherits keyword, it fixes.

的Web.config

<pages pageBaseType="[MyNamespace].WebViewPageBase">
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>

WebViewPageBase

public class WebViewPageBase : WebViewPage
{
    public SomeType MyProperty { get; set; }

    public override void InitHelpers()
    {
        base.InitHelpers();
        MyProperty = { foo };
    }

    public override void Execute()
    {

    }
}


public class WebViewPageBase<T> : WebViewPage<T>
{
    public SomeType MyProperty { get; set; }

    public override void InitHelpers()
    {
        base.InitHelpers();
        MyProperty = { foo };
    }

    public override void Execute()
    {

    }
}

管窥

@model TopMenuModel

<div class="topMenu">
@MyProperty
</div>

但在后我读过没有关于 @inherits 关键字指令。有没有办法,我错过任何事或任何方式,使无 @inherits 关键字这项工作中的所有页面?

But in the post I've read there is no instruction about @inherits keyword. Is there any thing that I miss or any way to make this work without @inherits keyword in all pages?

解决:

的web.config 文件是不正确的。我在的web.config改变基本类型文件下的查看目录,并将其固定。

web.config file in root directory is not the right one. I changed base type in the web.config file under View directory and it fixed.

推荐答案

为什么你展示 WebViewPageBase 的两个版本:通用和非通用

Why did you show two versions of WebViewPageBase: generic and non-generic?

您只需要在通用版:

public class MyWebView<T> : WebViewPage<T>
{
    public SomeType MyProperty { get; set; }

    public override void InitHelpers()
    {
        base.InitHelpers();
        MyProperty = new SomeType();
    }

    public override void Execute()
    {
    }
}

和则:

<pages pageBaseType="MvcApplication1.WebViews.MyWebView">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>

现在你的观点里,你将能够使用属性:

Now inside your views you will be able to use the property:

@model TopMenuModel

<div class="topMenu">
    @MyProperty
</div>


更新:

一步设置步骤:


  1. 创建使用互联网的模板创建一个新的ASP.NET MVC 3应用程序

  2. 添加自定义基本视图:

  1. Create a new ASP.NET MVC 3 application using the Internet Template
  2. Add a custom base view:

namespace MvcApplication1
{
    public class MyWebView<T> : WebViewPage<T>
    {
        public string MyProperty { get; set; }

        public override void InitHelpers()
        {
            base.InitHelpers();
            MyProperty = "Hello World";
        }

        public override void Execute()
        {
        }
    }
}


  • pageBaseType 属性〜/查看/ web.config中(不要与混淆〜/的web.config

  • Set the pageBaseType attribute in ~/Views/web.config (not to be confused with ~/web.config):

    <pages pageBaseType="MvcApplication1.MyWebView">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
    


  • 〜/查看/主页/ Index.cshtml 使用属性:

    <div>
        @MyProperty
    </div>
    


  • 按<大骨节病> Ctrl + F5键运行应用程序,如果一切顺利的话,你会以的Hello World 的欢迎。

  • Hit Ctrl+F5 to run the application and if everything goes well you will be greeted with a Hello World.

    这篇关于改变对MVC4基本视图类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆