Warm tip: This article is reproduced from serverfault.com, please click

MVC Enums and Models in one View

发布于 2021-10-14 14:27:38

I'm trying to use the model getting passed in the controller and a Enum in my view. I keep getting an error that the type is not valid in the given context. I am trying to use an extension method to get the display value from the enum. The model getting passed is a db model, therefore I don't want to add the enum to the model.

This is a partial of the View:

@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>
            }

The error is showing on the following div but it's complaining abut the enum.

I tried

@using Domain.Enums

@using RegistrationEnum = Domain.Enums.RegistrationProviders

and I aslo tried

@foreach (var provider in Domain.Enums.RegistrationProviders)

Enum Code:

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
    }
}

Extension Code:

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;
        }
    }
Questioner
JEuvin
Viewed
0
vivek nuna 2021-10-14 22:47:59

You should run foreach loop like this.

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

}