Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rowan Easter 5 posts 75 karma points
    Jul 22, 2020 @ 14:58
    Rowan Easter
    0

    Html Action error Object reference not set to an instance of an object

    Hi All, I'm trying to use a custom umbRegisterMemberController SurfaceController, which I've firstly just copied from the Umbraco source code and added a Child Action to. Having followed this guide https://our.umbraco.com/Documentation/Reference/Templating/Mvc/Forms/tutorial-child-action

    I've also created a register form that just has a @Html.EditorFor(x => Model)

    In the page where I call @Html.Action("RenderForm", "CustomerMembership") I just get the very generic error

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    
    Line 7:  <h1>@Model.Value("title")</h1>
    Line 8:  
    Line 9:  @Html.Action("RegisterForm", "CustomerMembership");
    

    Custom Surface Controller:

    using System;
    using System.Web.Mvc;
    using System.Web.Security;
    using Umbraco.Core;
    using Umbraco.Core.Cache;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Persistence;
    using Umbraco.Core.Services;
    using Umbraco.Web.Mvc;
    
    using Umbraco.Web.Models;
    
    public class CustomerMembershipController : SurfaceController
        {
            public CustomerMembershipController()
            {
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            [ValidateUmbracoFormRouteString]
            public ActionResult Register([Bind(Prefix = "registerModel")] RegisterModel model)
            {
                if (ModelState.IsValid == false)
                {
                    return CurrentUmbracoPage();
                }
    
                // U4-10762 Server error with "Register Member" snippet (Cannot save member with empty name)
                // If name field is empty, add the email address instead
                if (string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Email) == false)
                {
                    model.Name = model.Email;
                }
    
                //var test = model.IsValidated;
    
                MembershipCreateStatus status;
                var member = Members.RegisterMember(model, out status, model.LoginOnSuccess);
    
                switch (status)
                {
                    case MembershipCreateStatus.Success:
    
                        TempData["FormSuccess"] = true;
    
                        //if there is a specified path to redirect to then use it
                        if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
                        {
                            return Redirect(model.RedirectUrl);
                        }
                        //redirect to current page by default
    
                        return RedirectToCurrentUmbracoPage();
                    case MembershipCreateStatus.InvalidUserName:
                        ModelState.AddModelError((model.UsernameIsEmail || model.Username == null)
                            ? "registerModel.Email"
                            : "registerModel.Username",
                            "Username is not valid");
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        ModelState.AddModelError("registerModel.Password", "The password is not strong enough");
                        break;
                    case MembershipCreateStatus.InvalidQuestion:
                    case MembershipCreateStatus.InvalidAnswer:
                        // TODO: Support q/a http://issues.umbraco.org/issue/U4-3213
                        throw new NotImplementedException(status.ToString());
                    case MembershipCreateStatus.InvalidEmail:
                        ModelState.AddModelError("registerModel.Email", "Email is invalid");
                        break;
                    case MembershipCreateStatus.DuplicateUserName:
                        ModelState.AddModelError((model.UsernameIsEmail || model.Username == null)
                            ? "registerModel.Email"
                            : "registerModel.Username",
                            "A member with this username already exists.");
                        break;
                    case MembershipCreateStatus.DuplicateEmail:
                        ModelState.AddModelError("registerModel.Email", "A member with this e-mail address already exists");
                        break;
                    case MembershipCreateStatus.UserRejected:
                    case MembershipCreateStatus.InvalidProviderUserKey:
                    case MembershipCreateStatus.DuplicateProviderUserKey:
                    case MembershipCreateStatus.ProviderError:
                        //don't add a field level error, just model level
                        ModelState.AddModelError("registerModel", "An error occurred creating the member: " + status);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
    
                return CurrentUmbracoPage();
            }
    
            [ChildActionOnly]
            public ActionResult ShowCommentForm()
            {
                return PartialView("RegisterForm");
            }
    
        }
    

    RegisterForm.cshtml:

    @model Umbraco.Web.Models.RegisterModel
    
    
    @using (Html.BeginUmbracoForm<CustomerMembershipController>("Register"))
    {
        @Html.EditorFor(x => Model)
        <input type="submit" />
    }
    

    Register.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
    Layout = "master.cshtml";
    }
    
    <h1>@Model.Value("title")</h1>
    
    @Html.Action("RegisterForm", "CustomerMembership");
    
  • Nigel Wilson 945 posts 2077 karma points
    Jul 23, 2020 @ 01:38
    Nigel Wilson
    0

    Hi Rowan

    Shouldn't your action be

    @Html.Action("ShowCommentForm", "CustomerMembership");

    Cheers

    Nigel

  • Rowan Easter 5 posts 75 karma points
    Jul 23, 2020 @ 07:59
    Rowan Easter
    0

    Hi Nigel, Yeah cheers for that spot. I changed that method to ShowRegisterForm, and still have the same issue. Thanks, Rowan

  • Nigel Wilson 945 posts 2077 karma points
    Jul 23, 2020 @ 21:07
    Nigel Wilson
    0

    Hi Rowan

    Maybe try changing the ShowCommentForm method in your controller to the following:

    Umbraco.Web.Models.RegisterModel model = new Umbraco.Web.Models.RegisterModel();
    return PartialView("RegisterForm",model);
    

    The partial view is expecting a model, but your action isn't sending one, think this might solve.

    Cheers

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft