【问题标题】:MVC Enums and Models in one View - Error: "is a type, which is not valid in the given context"一个视图中的 MVC 枚举和模型 - 错误:“是一种类型,在给定的上下文中无效”
【发布时间】:2021-12-02 22:26:24
【问题描述】:

我正在尝试使用在控制器中传递的模型和在我的视图中的枚举。我不断收到类型在给定上下文中无效的错误。我正在尝试使用扩展方法从枚举中获取显示值。传递的模型是 db 模型,因此我不想将枚举添加到模型中。

这是视图的一部分:

@model Domain.Models.Customer
@using Domain.Enums


             @using (Html.BeginForm("CustomerEdit", "Customer", null, FormMethod.Post, false, null)) {
                @Html.HiddenFor(model => model.Idcustomer)
                <div class="row mb-2">
                    <div class="col-md-4">
                        @Html.LabelFor(model => model.Name, "Customer Name")
                    </div>
                    <div class="col-md-6">
                        @Html.TextBoxFor(model =>model.Name, new { @class="form-control"})
                    </div>
                </div>
                
                <div class="row mb-2">
                    <div class="col-md-4">
                        @Html.LabelFor(model => model.TimeZoneAdjustment, "Time Zone Adjustment")
                    </div>
                    <div class="col-md-6">
                        @Html.TextBoxFor(model =>model.TimeZoneAdjustment, new { @type="number", @class="form-control"})
                    </div>
                </div>   
                <div class="row mb-2">
                    <div class="col-md-4">
                        @Html.LabelFor(model => model.IndividualRegistrationProvider, "Individual Registration Provider")
                    </div>
                    <div class="col-md-6">
                       <select class="form-control" id="@Html.IdFor(model => model.IndividualRegistrationProvider)">
                            <option selected>Select Individual Provider</option>
                            @foreach (var provider in RegistrationProviders)
                            {
                              <option value="@provider.GetDescription()">@provider.GetDescription()</option>
                            }
                        </select>    
                    </div>
                </div>
                
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-primary" />
                </div>
            }

错误显示在以下 div 上,但它在枚举附近抱怨。

我试过了

@使用域.枚举

@using RegistrationEnum = Domain.Enums.RegistrationProviders

我也试过了

@foreach(Domain.Enums.RegistrationProviders 中的 var 提供者)

枚举代码:

namespace Domain.Enums
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;

    public enum RegistrationProviders
    {
        [Description("Team")]
        Team,

        [Description("Play")]
        Play,

        [Description("Blue Star")]
        BlueStar,

        [Description("Other")]
        Other,

        [Description("None")]
        None
    }
}

分机代码:

public static class EnumExtensions
    {
        public static string GetDescription<T>(this T enumValue) where T: struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                return null;
            }

            var description = enumValue.ToString();
            var fieldInfo = enumValue.GetType().GetField(description);

            if(fieldInfo != null)
            {
                var attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);

                if(attrs?.Length > 0)
                {
                    description = ((DescriptionAttribute)attrs[0]).Description;
                }
            }

            return description;
        }
    }

【问题讨论】:

  • 哪一行导致错误?
  • 错误来自枚举。它显示在不同的行上,但它抱怨枚举。
  • 但是贴出代码,你刚刚声明了枚举,没有在任何地方使用
  • @using 用于引用命名空间或静态上下文中的类型。仅仅引用一个类型而不给它一个别名是无效的。所以你想要@using Domain@using Enums=Domain.Enums
  • @JEuvin 是 Domain.Enums 枚举的命名空间吗?你能告诉我们你的枚举代码吗

标签: c# .net-core razor asp.net-mvc-5 model-view


【解决方案1】:

你应该像这样运行foreach循环。

@foreach(RegistrationProviders provider in Enum.GetValues(typeof(RegistrationProviders)))
{

}

【讨论】:

  • 这没有让我得到描述。这就是为什么我没有这样做。
  • 为什么需要通用?为什么where T: struct 为什么不where T: enum
  • 我真的不只是把它变成通用的,以防我们以后可能想对此进行扩展。
  • 我需要将@using Domain.Extensions 添加到视图中才能使扩展正常工作。
  • 谢谢Vivek。
猜你喜欢
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2016-03-30
  • 2018-05-07
  • 2020-04-16
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多