/// <summary> /// Enums the value info dropdown internal. /// </summary> /// <param name="html">The html. </param> /// <param name="expression"> The expression. </param> /// <param name="value"> The value. </param> /// <param name="isNullable"> The is Nullable. </param> /// <param name="choices"> The choices. </param> /// <returns> /// Mvc string /// </returns> internal static MvcHtmlString DropDownListEnumExtendedInfoInternal( HtmlHelper html, string expression, object value, bool isNullable, IEnumerable choices) { StringBuilder build = new StringBuilder(); build.AppendFormat("<select name='{0}'>", html.Encode(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression))); if (choices != null) { if (isNullable) { build.AppendOption(string.Empty, string.Empty, value == null); } foreach (var choice in choices) { build.AppendOption( Convert.ToInt32(choice), html.Encode(EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(choice).Name), object.Equals(value, choice)); } } build.Append("</select>"); return(MvcHtmlString.Create(build.ToString())); }
/// <summary> /// Creates the select list for. /// </summary> /// <typeparam name="T">Type of enum.</typeparam> /// <param name="selected">The selected.</param> /// <param name="title">The title.</param> /// <param name="value">Value of title.</param> /// <returns> /// Select list with enum possible values /// </returns> public static IList <SelectListItem> CreateSelectListItemsFor <T>(T selected, string title = null, string value = null) { Type type = typeof(T); var items = new List <SelectListItem>(); Array options = null; if (IsNullable(selected, type)) { items.Add(new SelectListItem { Text = title, Value = value, Selected = object.Equals(selected, null) }); options = Enum.GetValues(Nullable.GetUnderlyingType(type)); } else { options = Enum.GetValues(type); } foreach (var option in options) { int numericValue = Convert.ToInt32(option); var selectListItem = new SelectListItem { Text = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(option).Name, Value = numericValue.ToString(), Selected = object.Equals(selected, option) }; items.Add(selectListItem); } return(items); }
public void StringValue_SaveOrUpdateCopyPersistance() { var enumField = ExtendedTestEnum.Something; Assert.That(EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(enumField).CustomValue, Is.TypeOf <string>()); var model = new EnumExtendedModel(); model.SampleEnum = enumField; model.SampleNullableEnum = ExtendedTestEnum.Misc; this.Session.Save(model); this.Session.Evict(model); this.Session.SaveOrUpdateCopy(model); this.Session.Flush(); this.Session.Clear(); var modelFromDb = this.Session.Get <EnumExtendedModel>(model.Id); Assert.That(modelFromDb.SampleEnum, Is.EqualTo(enumField)); model.SampleEnum = ExtendedTestEnum.Pending; this.Session.SaveOrUpdateCopy(model); this.Session.Flush(); this.Session.Clear(); var modelFromDbTwo = this.Session.Get <EnumExtendedModel>(model.Id); Assert.That(modelFromDbTwo.SampleEnum, Is.EqualTo(ExtendedTestEnum.Pending)); }
/// <summary> /// Write an instance of the mapped class to a prepared statement. /// Implementors should handle possibility of null values. /// A multi-column type should be written to parameters starting from index. /// </summary> /// <param name="cmd">a IDbCommand</param> /// <param name="value">the object to write</param> /// <param name="index">command parameter index</param> /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception> public override void NullSafeSet(IDbCommand cmd, object value, int index) { if (value != null) { if (value is TEnum) { var customValues = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).GetCustomValues(); string result = null; if (customValues.Length != 0) { result = string.Join(this.separator, Array.ConvertAll(customValues, i => i.ToString())); } NHibernateUtil.String.NullSafeSet(cmd, result, index); } } }
/// <summary> /// Write an instance of the mapped class to a prepared statement. /// Implementors should handle possibility of null values. /// A multi-column type should be written to parameters starting from index. /// </summary> /// <param name="cmd">a IDbCommand</param> /// <param name="value">the object to write</param> /// <param name="index">command parameter index</param> /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception> public override void NullSafeSet(global::System.Data.IDbCommand cmd, object value, int index) { ((IDataParameter)cmd.Parameters[index]).Value = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).CustomValue ?? DBNull.Value; }
/// <summary> /// Write an instance of the mapped class to a prepared statement. /// Implementors should handle possibility of null values. /// A multi-column type should be written to parameters starting from index. /// </summary> /// <param name="cmd">a IDbCommand</param> /// <param name="value">the object to write</param> /// <param name="index">command parameter index</param> /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception> public virtual void NullSafeSet(IDbCommand cmd, object value, int index) { ((IDataParameter)cmd.Parameters[index]).Value = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).CustomValue; }