枚举不能通过表达式引用类型

时间:2014-10-24 08:16:07

标签: c# asp.net-mvc enums

我有模特课:

namespace BazeProjekt.Models
{
    public class Radio
    {
        public enum and_or { OR, AND };
        public enum algoritam { fuzzy, normal };

    }
}

还有我的观点:

@using Npgsql
@model BazeProjekt.Models.Radio

@{
    ViewBag.Title = "Search";
}

<div class="jumbotron">
    <h1>Full and fuzzy text search</h1>
</div>

<h2>Search:</h2>

@using (Html.BeginForm("Index", "Home"))
{
    @Html.TextBox("upit")
    <input type="submit" value="Traži" />
}

@Html.RadioButtonFor(x => x.and_or, "OR")
@Html.RadioButtonFor(x => x.and_or, "AND")

<h2>SQL:</h2>

@Html.TextArea("sql",(string)ViewBag.query,10,100,null)

@{

    NpgsqlDataReader dr = (NpgsqlDataReader)ViewBag.tablica;
}

@if (dr != null)
{
    int cnt = 0;

    while (dr.Read())
    {
        <p>@Html.Raw(dr[0]) - [@dr[1]]</p>
        cnt++;
    }

    <p>Broj pronađenih filmova je: @cnt</p>
}

我希望有2个带有OR和AND选项的单选按钮,但是编译器说它不能通过表达式引用类型。

错误1'和_或':不能通过表达式引用类型;尝试'BazeProjekt.Models.Radio.and_or'而不是c:\ Users \ TranceFusion \ Documents \ Visual Studio 2013 \ Projects \ Baze Projekt \ Baze Projekt \ Views \ Home \ Index.cshtml 20 29 Baze Projekt

1 个答案:

答案 0 :(得分:0)

您需要单独定义您的枚举,并在模型中包含该枚举的属性

public enum MyEnum
{
  OR,
  AND
}

public class Radio
{
  public MyEnum and_or { get; set; }
}

然后在视图中

@Html.RadioButtonFor(x => x.and_or, "OR")
@Html.RadioButtonFor(x => x.and_or, "AND")