Laden...

DropDownList in mvc 4

Erstellt von wickedcsharper vor 11 Jahren Letzter Beitrag vor 11 Jahren 608 Views
Thema geschlossen
wickedcsharper Themenstarter:in
160 Beiträge seit 2008
vor 11 Jahren
DropDownList in mvc 4

Hallo zusammen,

Ich habe ASP MVC 4.

Auch auf die Gefahr hin, dass dieses Thema hier mit Sicherheit schon diskuttiert wurde.

Ich habe folgende Klasse:

namespace WebTermin.Models
{
public class DVD
{
public int ID { get; set; }
public string Title { get; set; }
public int Year { get; set; }
public Rating rating { get; set; }
}

public enum Rating  
{  
    G, PG, PG13, R  
}  

}

Wie bekomme ich in der Create und Edit Seite eine DropDownliste
mit den Ratings hin.

Habe da was von Helper Klassen gehört.
Wo und wie baue ich die ein?

mfg

„Wenn man eine Katze auseinandernehmen will, um zu sehen, wie sie funktioniert, hat man als erstes eine nicht funktionierende Katze in den Händen.“

M
402 Beiträge seit 2005
vor 11 Jahren

Hi...

ich hab für so einen Fall eine kleine Helper-Klasse...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Comon
{

        public class EnumNameAttribute : Attribute
        {
            private string _name;

            public EnumNameAttribute(string Name)
            {
                _name = Name;
            }
            public string Name
            {
                get
                {
                    return _name;
                }
            }
        }

        public class EnumHelper
        {

            public static T Parse<T>(string value)
            {
                return (T)Enum.Parse(typeof(T), value);
            }

            public static string GetName<T>(string value)
            {
                return GetListItems<T>().Where(c => c.Key == value).First().Value;
            }

            public static Dictionary<string, string> GetListItems<T>()
            {
                var t_ = typeof(T);
                if (!t_.IsEnum)
                    throw new ApplicationException("GetListItems does not support non-enum types");

                Dictionary<string, string> list = new Dictionary<string, string>();
                foreach (FieldInfo field in t_.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
                {
                    int value;
                    string display;
                    string strvalue;

                    value = (int)field.GetValue(null);
                    display = Enum.GetName(t_, value);
                    strvalue = field.Name;
                    foreach (Attribute currAttr in field.GetCustomAttributes(true))
                    {
                        EnumNameAttribute valueAttribute = currAttr as EnumNameAttribute;
                        if (valueAttribute != null)
                            display = valueAttribute.Name;
                    }
                    list.Add(strvalue, display);
                }
                return list;
            }


        }
    
}

Damit kann man das dann so machen....


var RatingSelect = new SelectList(EnumHelper.GetListItems<Rating>(), "Key", "Value");

"Friendly-Names" für die Werte sind damit auch möglich (per Attribute)


public enum Rating
 {

[EnumName("Whatever")]
G, 
PG,
PG13,
R
 }

Hoffe das hilft dir weiter...

lg

16.835 Beiträge seit 2008
vor 11 Jahren

Ich hab jetzt zwar ein bisschen gezögert, aber wenn ich unsere Regeln ernst nehmen will, dann muss ich das Thema schließen.
Es sind einfach absolute Grundlagen in Sachen HTML und ASP MVC, das in den ersten Kapiteln jedes Tutorials genannt wird.

Thema geschlossen