/ / एमवीसी 3 एनम चयन सूची, प्रदर्शन एनोटेशन का उपयोग करके - asp.net-mvc, asp.net-mvc-3, विशेषताएँ, enums, डेटा-एनोटेशन

प्रदर्शन एनोटेशन का उपयोग करते हुए एमवीसी 3 एनम चयन सूची - asp.net-mvc, asp.net-mvc-3, विशेषताएँ, enums, डेटा-एनोटेशन

मेरे पास एक एचटीएमएल हेल्पर है जो एनम को एक सिलेक्टलिस्ट में परिवर्तित करता है:

public static HtmlString EnumSelectListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> forExpression,
object htmlAttributes,
bool blankFirstLine) where TModel : class where TProperty : struct
{
//MS, it its infinite wisdom, does not allow enums as a generic constraint, so we have to check here.
if (!typeof(TProperty).IsEnum) throw new ArgumentException("This helper method requires the specified model property to be an enum type.");

//initialize values
var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
var propertyName = metaData.PropertyName;
var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();

//build the select tag
var returnText = string.Format("<select id="{0}" name="{0}"", HttpUtility.HtmlEncode(propertyName));
if (htmlAttributes != null)
{
foreach (var kvp in htmlAttributes.GetType().GetProperties()
.ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
{
returnText += string.Format(" {0}="{1}"", HttpUtility.HtmlEncode(kvp.Key),
HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
}
}
returnText += ">n";

if (blankFirstLine)
{
returnText += "<option value=""></option>";
}

//build the options tags
foreach (var enumName in Enum.GetNames(typeof(TProperty)))
{
var idValue = ((int)Enum.Parse(typeof(TProperty), enumName, true)).ToString();
var displayValue = enumName;
var titleValue = string.Empty;
returnText += string.Format("<option value="{0}" title="{1}"",
HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
if (enumName == propertyValue)
{
returnText += " selected="selected"";
}
returnText += string.Format(">{0}</option>n", HttpUtility.HtmlEncode(displayValue));
}

//close the select tag
returnText += "</select>";
return new HtmlString(returnText);
}

समस्या यह है कि, Enums के पास उनके नामों में रिक्त स्थान नहीं हो सकते हैं, इसलिए यदि आप एक-शब्द enum मानों के अलावा कुछ और हैं, तो आप कुछ बदसूरत चयन सूचियां प्राप्त कर सकते हैं, जैसे:

public enum EmployeeTypes
{
FullTime = 1,
PartTime,
Vendor,
Contractor
}

अब, मेरे पास उज्ज्वल विचार था, "मुझे पता है! मैं इसके लिए डेटा एन्नोटेशन का उपयोग करूंगा!" ... इसलिए मैंने अपना enum इस तरह दिखता है:

public enum EmployeeTypes
{
[Display(Name = "Full Time")]
FullTime = 1,
[Display(Name = "Part Time")]
PartTime,
[Display(Name = "Vendor")]
Vendor,
[Display(Name = "Contractor")]
Contractor
}

... लेकिन अब मैं अपने सिर को खरोंच कर रहा हूं कि मेरे सहायक वर्ग में उन विशेषताओं को कैसे पहुंचाया जाए। क्या कोई मुझे इस पर जा सकता है?

उत्तर:

उत्तर № 1 के लिए 4

आप पढ़ सकते हैं DisplayAttribute लूप के अंदर प्रतिबिंब का उपयोग करके enum फ़ील्ड से:

foreach (var enumName in Enum.GetNames(typeof(TProperty)))
{
var idValue = ((int)Enum.Parse(typeof(TProperty), enumName, true)).ToString();
var displayValue = enumName;

// get the corresponding enum field using reflection
var field = typeof(TProperty).GetField(enumName);
var display = ((DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), false)).FirstOrDefault();
if (display != null)
{
// The enum field is decorated with the DisplayAttribute =>
// use its value
displayValue = display.Name;
}

var titleValue = string.Empty;
returnText += string.Format("<option value="{0}" title="{1}"",
HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
if (enumName == propertyValue)
{
returnText += " selected="selected"";
}
returnText += string.Format(">{0}</option>n", HttpUtility.HtmlEncode(displayValue));
}

जवाब के लिए 0 № 2

मैं हमेशा एनम क्लास के लिए विवरण विशेषता को पढ़ने के लिए एक विस्तार विधि बनाने और आपके पास जो कुछ भी है, उस पर ToDescription () को कॉल करने का प्रशंसक रहा हूं। ऐसा कुछ।

public static class EnumExtensions
{
public static String ToDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
}