Umbraco 5中@ Html.EditorFor的例外情况

时间:2012-03-06 08:40:31

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

我已经使用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 =&gt; contactForm)上得到一个Null引用异常。这是堆栈恍惚:

[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个小时,谷歌搜索和尝试不同的事情。如果有人知道如何解决这个问题,我将非常感激。

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

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

更新-3 最后,我使用了以下两个帖子中建议的方法。我使用这种方法的问题是我直接在Umbraco的网站上添加了SurfaceController,而不是在Umbraco的网站上创建另一个项目并引用它。当我创建单独的项目 - 控制器显示,一切都很好。我正在标记这个让我认为正确答案的帖子。

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

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

2 个答案:

答案 0 :(得分:1)

假设您发布了视图的完整代码,那么您的视图不会被强类型化,并且缺少导致EditorFor抛出异常的模型。只需将模型指令添加到视图中,然后从控制器中将对象推入视图中:

@model Site.Extensions.ViewModel.ContactViewModel

        [ChildActionOnly]
    public PartialViewResult ContactForm()
    {
        var model = new ContactViewModel();
        return PartialView(model);
    }

答案 1 :(得分:0)

不确定为什么本地化元数据提供程序会抛出异常,您可以发布模型,因为这可能是异常源自的地方。

无论如何,这是一个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" />
}

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