EPiServer MVC currentPage issues

3k views Asked by At

I'm currently having some problems with the following codesnippets which seems almost identical to me, but behaves differently.

These snippets are from two different projects I've been working on, and they are built the same way but only one of them works correctly.

These are the Forms where I enter the controllers:

Form 1, inside a twitter bootstrap dropdown menu, located in the _Layout file:

    @using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post))
    {
    <li>
        <button type="submit" class="dropdownButton">Redigera Profil</button>
    </li>
    }

Form 2, tried different locations but right now it's in a table in a Block view:

    <td>
        @using (Html.BeginForm("EditProfile", "ProfilePage", FormMethod.Post))
        {
            <button type="submit">Redigera profil</button>
        }
    </td>

Both seems pretty identical, right?

Now here are the controllers

Controller 1:

    public ActionResult EditProfile(ProfilePage currentPage)
    {
        var model = new ProfilePageViewModel(currentPage);
        model.CurrentUser = ConnectionHelper.GetUserInformationByEmail(User.Identity.Name);
        return View("EditProfile", model);
    }

Controller 2:

    public ActionResult EditProfile(ProfilePage currentPage)
    {
        ProfilePageViewModel model = new ProfilePageViewModel(currentPage);
        model.currentUser = ConnectionHelper.GetCurrentUserByEmail(User.Identity.Name);
        return View("EditProfile", model);
    }

Also pretty much identical.

I've added the same routing in both projects:

    protected override void RegisterRoutes(RouteCollection routes)
    {
        base.RegisterRoutes(routes);
        routes.MapRoute(
          "Default",                                             
          "{controller}/{action}/{id}",                           
          new { controller = "Home", action = "Index", id = "" });
    }

Now here's the problem:

Form 1 and controller 1 works perfectly and recieves the ProfilePage currentPage without any problems, but form 2 and controller 2 doesn't work and gets null value. As I stated earlier Form 1 is posted on the _Layout page and Form 2 is posted from a Block which is rendered within an mvc @section. I don't think this is the problem because I've tried to access the controller from different parts of the page, but it's not working anywhere - but in the other project it's working everywhere, which is driving me insane.

Does anyone have any idea why it is like this? I've stepped through both of them while debugging but the only difference is that one works and the other doesn't.

Thanks in advance

deSex

EDIT :

Here I render a section called "content", where almost everything will be rendered.

    <div id="content">
     @RenderBody()
     @RenderSection("content", false)
    </div>

My startpage has a ContentArea for blocks, rendered within this section:

@model Intranet.Models.ViewModels.StartPageViewModel
@section content{
   @if (User.Identity.IsAuthenticated)
   {
       <div class="contentArea">
           @Html.PropertyFor(x => x.MainContentArea)
       </div>
   }
}

And here is the controller that inherits from BlockController:

public class ProfileBlockController : BlockController<ProfileBlock>
{
    public override ActionResult Index(ProfileBlock currentBlock)
    {
        ProfileBlockViewModel model;
        if (currentBlock != null)
        {
            model = new ProfileBlockViewModel(currentBlock);
        }
        else
        {
            model = (ProfileBlockViewModel)Session["model"];
        }
        model.CurrentUser = ConnectionHelper.GetCurrentUserByEmail(User.Identity.Name);
        var availableStatuses = ConnectionHelper.GetAllOfficeStatuses();
        availableStatuses.Remove(model.CurrentUser.OfficeStatus);
        model.AvailableStatusChanges = availableStatuses;
        Session["model"] = model;

        return PartialView(model);
    }

}
1

There are 1 answers

7
Ted Nyberg On

The "currentPage" route value (i.e. parameter) will only be set by EPiServer's page route. It will always be null in a block controller.

However, you can get the page of the current request in a block controller with:

PageRouteHelper.Page

If the block is being rendered as part of a request for a profile page, you'll be able to get that profile page through PageRouteHelper.