簡體   English   中英

在ASP.net MVC 5中為模型創建隱藏字段時出錯

[英]Error while creating hidden field for model in ASP.net MVC 5

我試圖在部分視圖中為模型渲染一個隱藏字段,這是一個枚舉

我的代碼是

@model App.PrivacyLevelEnum
@Html.HiddenFor(m=>m);

我已經檢查過模型不是空的但我在渲染視圖時遇到了跟蹤錯誤

Value cannot be null or empty.\r\nParameter name: name

Stack trace

   at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, String format, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.InputExtensions.HiddenHelper(HtmlHelper htmlHelper, ModelMetadata metadata, Object value, Boolean useViewData, String expression, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.InputExtensions.HiddenFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression, IDictionary`2 htmlAttributes)
   at System.Web.Mvc.Html.InputExtensions.HiddenFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression)
   at ASP._Page_Views_Profile_PrivacyLevel_cshtml.Execute() in c:\TFS\DEFAULTCOLLECTION\Gac.Hr\Development\Source\Gac.Hr.Web.Html5\Views\Profile\PrivacyLevel.cshtml:line 58
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.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.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at ASP._Page_Views_Profile_ProfileDetailsEditor_cshtml.Execute() in c:\TFS\DEFAULTCOLLECTION\Gac.Hr\Development\Source\Gac.Hr.Web.Html5\Views\Profile\ProfileDetailsEditor.cshtml:line 107
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.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 GacHrUI.Controllers.ProfileController.StringifyView(String viewName, Object model) in c:\TFS\DEFAULTCOLLECTION\Gac.Hr\Development\Source\Gac.Hr.Web.Html5\Controllers\ProfileController.cs:line 62
   at GacHrUI.Controllers.ProfileController.RenderEditMode() in c:\TFS\DEFAULTCOLLECTION\Gac.Hr\Development\Source\Gac.Hr.Web.Html5\Controllers\ProfileController.cs:line 35
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass46.b__3f()

快速幫助將非常感激

由於model數據類型是枚舉而不是具有屬性的類,因此在顯示中使用時必須指定名稱和ID。

@Html.Hidden("privacyLevel", Model, new { @id="privacyLevel"})

你需要指定屬性名稱:

HiddenFor(m => m.yourPorperty)

這里的問題不是空模型,而是HiddenFor屬性的錯誤用法

方法的簽名是:

HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>) 

其中Expression<Func<TModel, TProperty>>指定要為其生成隱藏輸入的模型屬性。 此表達式類似於model => model.Name 如果我長話短說,那么這個表達式將用於為生成的html輸入生成nameid屬性

在您的情況下,您提供的表達式生成一個無效的空名稱。 這是拋出異常的方法(來自MVC源代碼):

 private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes)
        {
            string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            if (String.IsNullOrEmpty(fullName))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
            } 
          ... 
         }

現在可能的解決方案:

1)使用[HiddenInput(DisplayValue = false)]MSDN )在模型中裝飾你的屬性 - 這樣你就不必創建局部視圖了。 用法:

2)創建EditorTemplate 例如,您可以將其HiddentEnum.cshtmlHiddentEnum.cshtml (確保將其存儲在Views/Shared/EditorTemplates文件夾下):

  @model Enum

  @Html.HiddenFor(model=>model)

用法:

 @Html.EditorFor(model => model.TestEnum, templateName: "HiddenEnum")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM