简体   繁体   中英

Exception in @Html.EditorFor in Umbraco 5

I have made e installation with Umbraco version 5 and I am making a form with Surface Controller. I added the form in a view like that:

@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>

and Controller:

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!"} };
    }
}

And Model:

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

    public string LastName { get; set; }

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

    public string Message { get; set; }
}

And I get a Null reference exception at @Html.EditorFor(x => contactForm). This is the stack trance:

[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

I've been struggling with this error for 4-5 hours, googling and trying different things. If anyone has an idea how to fix this I would be very grateful.

Update I've posted the whole code of the view and the controller for more info.

Update-2 Added the model for more info.

Update-3 Finally I used the approach suggested in the two posts below. The problem I was having with the approach was that I added the SurfaceController directly in Umbraco's website instead of making another project and referencing it in Umbraco's website. When I created the separate project - Controller Showed Up and everything was fine. I am marking the post that let me to that thought as the right answer.

I couldn't resolve the problem with the exception in Umbraco's localization thought.

You can find the source code about this thread at the GitHub repository .

Assuming you're posting the complete code of your view your view is not strongly typed and missing a model causing the EditorFor to throw an exception. Just add the model directive to your view and from your controller push in an object into the view:

@model Site.Extensions.ViewModel.ContactViewModel

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

Not sure why the localization metadata provider is throwing an exception, can you post your Model as this will probably be where the exception is stemming from.

In any case, here's an example that def works:

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});
    }

}

And the view to render:

@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" />
}

Your code seems to be ok, so i'm wondering about your Model class. Also, it seems very strange that you are returning a JSON result when your form is successful. That means your browser will just display some JSON, pretty sure that's not what you are intending?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM