这是我的 View 模型:
public class EntityViewModel
{
[Required(ErrorMessage = "Title is required")]
[StringLength(255)]
[DisplayName("Title")]
public string Title { get; set; }
[Required(ErrorMessage = "Description is required")]
[DisplayName("Description")]
public string Description { get; set; }
[Required]
public DateTime StartTime { get; set; }
[Required]
public DateTime EndTime { get; set; }
[Required]
public Decimal InstantSellingPrice { get; set; }
public Nullable<Decimal> ShippingPrice { get; set; }
public Int64 Views { get; set; }
public Int32 UserId { get; set; }
public int RegionId { get; set; }
public short SelectCategoryId { get; set; }
public SelectList Categories { get; set; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
public Condition? Condition { get; set; }
}
public enum Condition
{
New=1,
Used=2
}
这是我在 Controller 中创建的操作:
public ActionResult Create()
{
ViewBag.DropDownList = ReUzze.Helpers.EnumHelper.SelectListFor<Condition>();
var model = new ReUzze.Models.EntityViewModel
{
Categories = new SelectList(this.UnitOfWork.CategoryRepository.Get(), "Id", "Name")
};
return View(model);
}
在我的创建 View 中:
<div class="form-group">
@Html.LabelFor(model => model.Condition)
@Html.DropDownListFor(model => model.Condition, ViewBag.DropDownList as SelectList, null)
</div>
我正在使用您可以找到的 Enumhelper here .
但现在我总是在创建 View 时出现这个错误:
@Html.DropDownListFor(model => model.Condition, ViewBag.DropDownList as SelectList, null)
错误:
Error 1 The call is ambiguous between the following methods or properties: 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, string)' and 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, System.Collections.Generic.IDictionary)' c:\Users\Niels\Documents\Visual Studio 2012\Projects\ReUzze\ReUzze\Views\Entity\Create.cshtml 57 30 ReUzze
最佳答案
我通常使用这样的代码。
public static class Enums {
public static IList<SelectListItem> SelectListOf<TEnum>(bool empty = false)
{
var type = typeof(TEnum);
if (type.IsEnum)
{
var list = Enum.GetValues(type)
.Cast<TEnum>()
.OrderBy(x => x)
.Select(x => new SelectListItem { Text = GetDescription(x), Value = x.ToString() })
.ToList();
if (empty)
{
list.Insert(0, new SelectListItem());
}
return list;
}
return new List<SelectListItem>();
}
private static string GetDescription(object enumerator)
{
try
{
//get the enumerator type
Type type = enumerator.GetType();
//get the member info
MemberInfo[] memberInfo = type.GetMember(enumerator.ToString());
//if there is member information
if (memberInfo != null && memberInfo.Length > 0)
{
//we default to the first member info, as it's for the specific enum value
object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
//return the description if it's found
if (attributes != null && attributes.Length > 0)
return ((DescriptionAttribute)attributes[0]).Description;
}
//if there's no description, return the string value of the enum
return enumerator.ToString();
}
catch (Exception e)
{
return string.Empty;
}
}
}
然后你可以像这样使用它:
Conditions = Enums.SelectListOf<Condition>();
关于mysql - 在 DropDownListFor 中使用枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684896/