@ Html.EnumDropDownListFor()附加信息:返回类型' System.Int32'不受支持

时间:2015-11-09 21:29:12

标签: asp.net-mvc razor

@ Html.EnumDropDownListFor()抛出错误

  

类型' System.ArgumentException'的例外情况发生在System.Web.Mvc.dll中但未在用户代码中处理

     

其他信息:返回类型' System.Int32'不受支持。"

视图中的代码

  <div class="form-group">
        <div class="col-md-10">
             @Html.Label("Importance Level")
             @Html.EnumDropDownListFor(model => model.NoticeCategory)
            <span class="help-block">...</span>
         </div>
    </div>

枚举类的代码

namespace OfficiumWebApp.Models.Enums
{
public enum NoticeCategories : byte
{
    [Display(Name= "High")]
    Hige = 1,

    [Display(Name = "Medium")]
    Medium = 2,

    [Display(Name = "Low")]
    Low = 3
}

我想在下拉列表中显示名称,因此我不确定它是否尝试返回System.int32。

问题出在哪里?

由于

编辑:忘记包含我的视图模型代码

    [Display(Name = "Notice Category")]
    public NoticeCategories? NoticeCategory  { get; set; }

编辑2:堆栈跟踪

 at System.Web.Mvc.Html.SelectExtensions.EnumDropDownListFor[TModel,TEnum](HtmlHelper`1 htmlHelper, Expression`1 expression, String optionLabel, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.SelectExtensions.EnumDropDownListFor[TModel,TEnum](HtmlHelper`1 htmlHelper, Expression`1 expression, String optionLabel)
   at System.Web.Mvc.Html.SelectExtensions.EnumDropDownListFor[TModel,TEnum](HtmlHelper`1 htmlHelper, Expression`1 expression)
   at ASP._Page_Views_NoticeBoard_Create_cshtml.Execute() in .......................................................
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

2 个答案:

答案 0 :(得分:0)

枚举返回为字符串列表。我猜你以前用一个返回int值的枚举代替了return。删除表并重新进行数据库表的创建,而无需枚举即可进行迁移。该问题将消除。它取代了您以前的模型。

OR

在表达式的返回属性上添加对ToString的调用。

答案 1 :(得分:-1)

我在通知类中将NoticeCategory定义为int。我将其更改为IEnumerable