gpt4 book ai didi

c# - 在 ASP.net MVC 5 中为模型创建隐藏字段时出错

转载 作者:行者123 更新时间:2023-11-30 12:43:09 25 4
gpt4 key购买 nike

我正在尝试在部分 View 中为枚举模型呈现一个隐藏字段

我的代码是

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

我已经检查过模型不为空,但在渲染 View 时出现以下错误

Value cannot be null or empty.\r\nParameter name: nameStack 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()

快速帮助将不胜感激

最佳答案

您的问题不是空模型,而是 HiddenFor 的错误使用属性

方法的签名是:

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

哪里Expression<Func<TModel, TProperty>>指定您要为其生成隐藏输入的模型属性。这个表达式看起来像 model => model.Name .如果我长话短说,那么这个表达式将用于生成 nameid生成的 html 输入的属性

在您的情况下,您提供的表达式会生成一个无效的空名称。这是抛出异常的方法(来自 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)] 在模型中装饰你的属性(property)( MSDN )- 这样您就不必创建局部 View 了。用法:

2) 创建一个 EditorTemplate .你可以称它为HiddentEnum.cshtml为了示例(确保将其存储在 Views/Shared/EditorTemplates 文件夹下):

  @model Enum

@Html.HiddenFor(model=>model)

用法:

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

关于c# - 在 ASP.net MVC 5 中为模型创建隐藏字段时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com