【问题标题】:Exception in @Html.EditorFor in Umbraco 5Umbraco 5 中 @Html.EditorFor 中的异常
【发布时间】:2012-03-06 08:40:30
【问题描述】:

我已经使用 Umbraco 版本 5 进行了电子安装,并且正在使用 Surface Controller 制作表单。我在这样的视图中添加了表单:

@inherits RenderViewPage
@using System.Web.Mvc.Html;
@using Umbraco.Cms.Web;
@using Umbraco.Cms.Web.Editors;
@{
    Layout = "Page.cshtml";
}

<section>
  @using (Html.BeginUmbracoForm("SendContactInfo", "ContactSurface"))
  {

      var contactForm = new ContactModel();


    @Html.ValidationSummary(prefix: "contactForm")

    @Html.EditorFor(x => contactForm)


    <input type="submit"  value="Send" />
  }
</section>

和控制器:

public class ContactSurfaceController:SurfaceController
{
    [HttpPost]
    public ActionResult SendContactInfo(
        [Bind(Prefix = "contactForm")]
            ContactModel model)
    {
        if (!ModelState.IsValid)
        {
            return CurrentUmbracoPage();
        }


        return new JsonResult() { Data = new { Name = "Sucess!"} };
    }
}

和型号:

public class ContactModel
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    [Required]
    public string Email { get; set; }

    public string Message { get; set; }
}

我在@Html.EditorFor(x => contactForm) 处得到一个空引用异常。这是堆栈恍惚:

[NullReferenceException: Object reference not set to an instance of an object.]


Umbraco.Framework.Localization.Web.Mvc.LocalizingModelMetadataProvider.GetConventionKeyNames(Type containerType, String propertyName, Boolean testSimplePropertyName) +285
   Umbraco.Framework.Localization.Web.Mvc.LocalizingModelMetadataProvider.CreateMetadata(IEnumerable`1 attributes, Type containerType, Func`1 modelAccessor, Type modelType, String propertyName) +597
   Umbraco.Cms.Web.Mvc.Metadata.UmbracoModelMetadataProvider.CreateMetadata(IEnumerable`1 attributes, Type containerType, Func`1 modelAccessor, Type modelType, String propertyName) +62
   System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, PropertyDescriptor propertyDescriptor) +93
   System.Web.Mvc.<GetMetadataForPropertiesImpl>d__2.MoveNext() +226
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
   System.Linq.<GetEnumerator>d__0.MoveNext() +96
   System.Linq.WhereEnumerableIterator`1.MoveNext() +87
   System.Web.Mvc.Html.DefaultEditorTemplates.ObjectTemplate(HtmlHelper html, TemplateHelperDelegate templateHelper) +368
   System.Web.Mvc.Html.DefaultEditorTemplates.ObjectTemplate(HtmlHelper html) +47
   System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +714
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1117
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +66
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +117
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +100
   System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper`1 html, Expression`1 expression) +56
   ASP._Page_Views_Umbraco_contact_cshtml.Execute() in e:\3fa4128e3b304b75b56af47f1ff5219e\Umbraco5-Smallsite\Views\Umbraco\contact.cshtml:17
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

我已经为这个错误苦苦挣扎了 4-5 个小时,在谷歌上搜索并尝试了不同的东西。如果有人知道如何解决这个问题,我将不胜感激。

更新 我已经发布了视图和控制器的完整代码以获取更多信息。

Update-2 添加模型以获取更多信息。

Update-3 最后我使用了下面两篇文章中建议的方法。我在使用这种方法时遇到的问题是我直接在 Umbraco 的网站中添加了 SurfaceController,而不是制作另一个项目并在 Umbraco 的网站中引用它。当我创建单独的项目时 - 控制器出现了,一切都很好。我正在将让我想到该想法的帖子标记为正确答案。

我无法解决 Umbraco 本地化思想中的异常问题。

您可以在GitHub repository 找到有关此线程的源代码。

【问题讨论】:

    标签: asp.net-mvc-3 razor umbraco


    【解决方案1】:

    假设您要发布视图的完整代码,您的视图不是强类型的,并且缺少导致 EditorFor 引发异常的模型。只需将模型指令添加到您的视图中,然后从您的控制器将一个对象推送到视图中:

    @model Site.Extensions.ViewModel.ContactViewModel
    
            [ChildActionOnly]
        public PartialViewResult ContactForm()
        {
            var model = new ContactViewModel();
            return PartialView(model);
        }
    

    【讨论】:

    • 我已经用整个视图和控制器更新了描述。我无法像您建议的那样添加模型,因为视图继承自 Umbraco 的 RenderViewPage
    • 您可以像我建议的那样创建您的 Contactform 视图:使用 ContactForm 模型强输入。不要从 RenderViewPage 继承。在 Umbraco 5 中使用 ContactForm ChildAction 创建一个宏,它将呈现您的视图。
    • 你也可以在你的模板中加入:@Umbraco.RenderMacro("ContactForm") 来渲染你的视图
    • 我为我的控制器创建了一个 ChildAction(如上所示),但是当我创建宏时它没有出现在列表中。你知道我做错了什么吗?
    • 如果您通过插件/包使您的控制器可用,那么您应该在您的控制器类上放置一个带有 guid 的属性,例如:[Surface("BB45D8A4-DD88-444E-89A6-A64351EFBE6A")] .如果您没有使用插件,请检查命名约定和 ChildActionOnly 属性。一定要编译并检查你是否可以在你的程序集中看到你的控制器。它应该出现在 Umbraco
    【解决方案2】:

    不确定本地化元数据提供程序为何抛出异常,您能否发布您的模型,因为这可能是异常的来源。

    无论如何,下面是一个 def 工作的例子:

    public class MyLocalSurfaceController : SurfaceController
    {
    
         public ActionResult HandleForm(
            [Bind(Prefix = "MyForm")]MyFunModel model)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
    
            return RedirectToCurrentUmbracoPage();
        }
    
        [ChildActionOnly]
        public PartialViewResult RenderForm(string name, int age)
        {
            return PartialView(new MyFunModel(){ Name = name, Age = age});
        }
    
    }
    

    以及要渲染的视图:

    @model Umbraco.Cms.Web.UI.Models.MyFunModel
    
    values passed in:
    <p>
        Age: @Model.Age<br/>
        Name: @Model.Name
    </p>
    
    @using(Html.BeginUmbracoForm("HandleForm", "MyLocalSurface"))
    {
        @Html.ValidationSummary(prefix: "MyForm")
        @Html.EditorFor(x => Model, "", "MyForm")
        <input type="submit" />
    }
    

    您的代码似乎没问题,所以我想知道您的模型类。此外,当您的表单成功时返回 JSON 结果似乎很奇怪。这意味着您的浏览器只会显示一些 JSON,很确定这不是您想要的?

    【讨论】:

    • 不,这不是我想要的。这只是为了测试动作是否成功结束。我更新了原始线程以包含模型。此外,如果您想查看生成它的实际代码,它位于HERE 中。无论如何我都会尝试你提出的方法
    猜你喜欢
    • 2015-06-07
    • 2013-08-23
    • 2015-07-05
    • 1970-01-01
    • 2013-04-17
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多