我如何在MVC3的剃刀页上使用枚举?

时间:2022-12-08 13:21:06

I declared an enum:

我声明一个枚举:

public enum HeightTypes{    Tall,    Short}

Now I want to use it on my razor page like this:

现在我想在我的剃刀页面上这样使用它:

@if (Model.Meta.Height == HeightTypes.Tall)

But there's a problem as I get an error. Is there some way I can tell the razor page about my enum?

但有个问题,我出错了。有什么办法可以告诉剃刀页面我的enum?

3 个解决方案

#1


16  

You have an error in your enum declaration (remove the trailing ;):

在enum声明中有一个错误(删除末尾;):

public enum HeightTypes { Short = 0, Tall = 1 }

then the following test should work:

那么下面的测试应该是有效的:

@if (Model.Meta.Height == HeightTypes.Tall)
{

}

you just have to make sure that your view is strongly typed and that you have brought into scope the namespace in which the Height enum is defined:

您只需确保您的视图是强类型的,并且您已经将定义高度enum的名称空间引入范围:

@using SomeAppName.Models
@model SomeViewModel

or reference the enum like this:

或者像这样引用全会:

@if (Model.Meta.Height == SomeAppName.Models.HeightTypes.Tall)
{

}

But to avoid doing this in all your razor views that require using this enum, it is easier to declare it in the <namespaces> section in the ~/Views/web.config:

但是,为了避免在所有需要使用此枚举的剃刀视图中这样做,可以更容易地在~/ views /web.config中的 部分声明它:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="SomeAppName.Models" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

#2


10  

Just do give a start-to-finish example:

只要给出一个从头到尾的例子:

C# CS Page

c# CS页面

namespace MyProject.Enums
{
    public enum CurveBasis
    {
        Aggregates,
        Premium
    }
}

Razor View

Razor视图

@using MyProject.Enums

<select id="dlCurveBasis">
    <option value="@CurveBasis.Aggregates">Aggregates</option>
    <option value="@CurveBasis.Premium">Premium</option>
</select>

#3


0  

You aren't specific about the exception, so I'm guessing this is a namespace issue. Add

您没有特定于异常,所以我猜测这是一个名称空间问题。添加

@using The.Namespace.Of.Your.Enum;

at the top. You can also specify namespaces to add automatically in /Views/web.config if you are going to use that namespace a lot:

在顶部。您还可以指定名称空间来自动添加/视图/web。配置,如果你打算经常使用这个命名空间:

<system.web.webPages.razor>
    ...
    <pages ...>
        <namespaces>
            <add namespace="System.Web" />
            ...
            <add namespace="The.Namespace.Of.Your.Enum" />

#1


16  

You have an error in your enum declaration (remove the trailing ;):

在enum声明中有一个错误(删除末尾;):

public enum HeightTypes { Short = 0, Tall = 1 }

then the following test should work:

那么下面的测试应该是有效的:

@if (Model.Meta.Height == HeightTypes.Tall)
{

}

you just have to make sure that your view is strongly typed and that you have brought into scope the namespace in which the Height enum is defined:

您只需确保您的视图是强类型的,并且您已经将定义高度enum的名称空间引入范围:

@using SomeAppName.Models
@model SomeViewModel

or reference the enum like this:

或者像这样引用全会:

@if (Model.Meta.Height == SomeAppName.Models.HeightTypes.Tall)
{

}

But to avoid doing this in all your razor views that require using this enum, it is easier to declare it in the <namespaces> section in the ~/Views/web.config:

但是,为了避免在所有需要使用此枚举的剃刀视图中这样做,可以更容易地在~/ views /web.config中的 部分声明它:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="SomeAppName.Models" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

#2


10  

Just do give a start-to-finish example:

只要给出一个从头到尾的例子:

C# CS Page

c# CS页面

namespace MyProject.Enums
{
    public enum CurveBasis
    {
        Aggregates,
        Premium
    }
}

Razor View

Razor视图

@using MyProject.Enums

<select id="dlCurveBasis">
    <option value="@CurveBasis.Aggregates">Aggregates</option>
    <option value="@CurveBasis.Premium">Premium</option>
</select>

#3


0  

You aren't specific about the exception, so I'm guessing this is a namespace issue. Add

您没有特定于异常,所以我猜测这是一个名称空间问题。添加

@using The.Namespace.Of.Your.Enum;

at the top. You can also specify namespaces to add automatically in /Views/web.config if you are going to use that namespace a lot:

在顶部。您还可以指定名称空间来自动添加/视图/web。配置,如果你打算经常使用这个命名空间:

<system.web.webPages.razor>
    ...
    <pages ...>
        <namespaces>
            <add namespace="System.Web" />
            ...
            <add namespace="The.Namespace.Of.Your.Enum" />