Handling a ASP.NET MVC ActionLink click within a DNN Module

The problem: DNN is taking care of routing,  that means it is not possible to use System.Web.Mvc.HtmlHelper to create the right URL that you want. DNN will not understand the URL’s created by the standard Microsoft Html or Url helper. The DNN module is embedded in a DNN page and the we need some additional information to access the controller that it part of the module in the page. A page can also include multiple modules. We need to retrieve that information from DNN and DNN includes its own versions of the Html and Url helpers provide that information. This example shows how you can use it,

How to use DotNetNuke.Web.Mvc.Helpers.HTMLhelper and DotNetNuke.Web.Mvc.Helpers.DnnUrlHelper to call the Action functions in your MVC controller

With the (DotNetNuke.Web.Mvc.Helpers) DnnHtmlHelper

The controller:

In this example, “FixedData” is the name of the View (that contains index.cshtml) and the prefix of the controller.

  • The controller contains the default Index action method. That clear the message we are displaying in the view
  • The controller contains a new action method that must be called when a button is pressed. It creates a entry from Message in the TempData.
  • We use TempData because TempData is used to pass data from the current request to the next request. It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action.
public class FixedDataController : DnnController
{
   public ActionResult Index()
   {
      ViewData["Message"] = "";
      return View();
   }
}

public ActionResult ButtonClick()
{
   ViewData["Message"] = "Are you sure?";
   return View("Index");
}

The view:

  • The ViewContent and ViewDataContainer information we get from DNN by inheriting our view from the class DnnWebViewPage.
  • And the namespace DotNetNuke.Web.Mvc.Helpers is the the DNN Helper classes.
@inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage
@using DotNetNuke.Web.Mvc.Helpers

DNN HTML Helper

@{
   var dnnHtmlHelper = new DotNetNuke.Web.Mvc.Helpers.DnnHtmlHelper(Dnn.ViewContext, Dnn.ViewDataContainer);
   @dnnHtmlHelper.ActionLink("Execute", "ButtonClick", "FixedData");
   <div>
      @ViewData["Message"];
   </div>
}

With the (DotNetNuke.Web.Mvc.Helpers) DnnUrlHelper

With help of the URL Helper we can use the same controller. But, for example, usa a button in Razor style.

The view:

@inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage

@using DotNetNuke.Web.Mvc.Helpers

DNN URL Helper
<p>
@{
    var dnnUrlHelper = new DotNetNuke.Web.Mvc.Helpers.DnnUrlHelper(Dnn.ViewContext);
    <input id="Button1" type="button" value="Execute" onclick="location.href='@dnnUrlHelper.Action("ButtonClick", "FixedData")'" />
    <div>
        @TempData["Message"];
    </div>

}

Leave a comment