Copied to clipboard

Flag this post as spam?

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


  • Tiaan Pat 4 posts 75 karma points
    Jun 01, 2016 @ 08:39
    Tiaan Pat
    0

    Getting MVC 5 Template working with Umbraco 7.4.3

    Hi All

    I have been playing around with Umbraco 7 for the past 2 months now and I am struggling to get the MVC 5 Template Pages to work correctly. The Template comes with Account and Manage Controllers with builtin Login/Register etc. functionality. I am using Macros for each of the pages that renders views and converted the controllers to be Surface Controllers.

    I got the Login and Register parts working with lots of frustration and now I am moving on to the Manage side of the app. I can load my Manage/Index page but when I try and load the ManageLogins page the macro fails to load.

    I think it has something to do with the fact that most of the Action Results are asynchronous but it doesn't make sense why the Manage/Index page loads as it's exactly the same as the ManageLogins action result.

    I have made sure that I return PartialViews for all the actions that returns views and also the models that goes with the pages.

    So here is the code for my Controller and Action:

        //
        // GET: /Manage/Index
        public async Task<ActionResult> Index(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                : message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                : "";
    
            var userId = User.Identity.GetUserId();
            var model = new IndexViewModel
            {
                HasPassword = HasPassword(),
                PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId)
            };
            return PartialView(model);
        }
    

    The controller inherits from SurfaceController

    ManageController : SurfaceController

    My Model:

    public class IndexViewModel : BaseUmbracoModel
    {
        public bool HasPassword { get; set; }
        public IList<UserLoginInfo> Logins { get; set; }
        public string PhoneNumber { get; set; }
        public bool TwoFactor { get; set; }
        public bool BrowserRemembered { get; set; }
    }
    

    BaseUmbracoModel:

    public class BaseUmbracoModel : RenderModel
    {
        public BaseUmbracoModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent, UmbracoContext.Current.PublishedContentRequest.Culture) {  }
    }
    

    And then my View:

    @model Platform.Umbraco.Web.Models.IndexViewModel
    @{ ViewBag.Title = "Manage";}
    
    <h2>@ViewBag.Title.</h2>
    
    <p class="text-success">@ViewBag.StatusMessage</p>
    <div>
    <h4>Change your account settings</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>Password:</dt>
        <dd>
            [
            @if (Model.HasPassword)
            {
                @Html.ActionLink("Change your password", "ChangePassword",null, new { Href = "/Manage/ChangePassword" })
            }
            else
            {
                @Html.ActionLink("Create", "SetPassword", null, new { Href = "/Manage/SetPassword" })
            }
            ]
        </dd>
        <dt>External Logins:</dt>
        <dd>
            @Model.Logins.Count [
            @Html.ActionLink("Manage", "ManageLogins", null, new { Href = "/Manage/ManageLogins" }) ]
        </dd>
        <dt>Two-Factor Authentication:</dt>
        <dd>
            @if (Model.TwoFactor)
                {
                    using (Html.BeginUmbracoForm("DisableTwoFactorAuthentication", "Manage", null, new { @class = "form-horizontal", role = "form" }))
                    {
                        @Html.AntiForgeryToken()
                        <text>Enabled
                        <input type="submit" value="Disable" class="btn btn-link" />
                        </text>
                    }
                }
                else
                {
                    using (Html.BeginUmbracoForm("EnableTwoFactorAuthentication", "Manage", null, new { @class = "form-horizontal", role = "form" }))
                    {
                        @Html.AntiForgeryToken()
                        <text>Disabled
                        <input type="submit" value="Enable" class="btn btn-link" />
                        </text>
                    }
                }
        </dd>
    </dl>
    

    And lastly the macro that loads the Manage/Index page:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @Html.Action("Index","Manage")
    

    Now all of that works but when I try and navigate from the Manage/Index page to the ManageLogins page then I get this error:

    An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
    
    Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
    

    Now I don't understand why the Manage/Index page is working but the ManageLogins page is not working.

    Has anyone got the standard pages of an MVC 5 app to work correctly? Is there anything I am doing wrong?

    I even followed this post for a possible solution but still nothing.

    Regards Tiaan

Please Sign in or register to post replies

Write your reply to:

Draft