DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Render Action in other controller based on setting

Sort:
You are not authorized to post a reply.





New Around Here





    hi.

    i want to render specific action in other controller in dnn.

    how can i do that?

    in my view i write this code:

    Html.RenderPartial is just for render partial in same controller.but not work for render action in other controller

          Html.RenderPartial("N_Notary/Index");

    where N_Notary is my controller and index is my action.

     

      






    New Around Here





      Is this in a MVC Module? You should be able to use
       Url.Action 
      which takes an input of the controller name and action you want invoke. If you just want to render the view itself the code you supplied should work, however you may need to pass a model into it if your "N_Notary/Index" view requires one





      New Around Here





        yes it is mvc module.

        URL.Action just create url of action for me.not render the action

        what i want to do is this:based on setting of module i need to show specific action

        var DefaultStartAction = Dnn.ModuleContext.Configuration.ModuleSettings.GetValueOrDefault("NotaryStartAction", "");
            if (!string.IsNullOrEmpty(DefaultStartAction))
            {

                Html.RenderPartial("N_Notary/Index");
               
            }
            else
            {
                  Html.RenderPartial("N_Notary/Index2");
            }






        New Around Here





          I think I understand what you are asking, the ability to choose what action to render from the cshtml (View Code). This violates a core principal of Model-View-Controller development and the logic belongs in the Controller where the View is a sample UI that just renders the Model.

          I understand that is not the answer you were hoping for and that we all have client demands. If you want to achieve this you won't be able to reference the Controllers Action such as "N_Notary/Index2". Instead find the view that "N_Notary/Index2" uses and reference it directly in the Html.RenderPartial() function.

          If that doesn't work you may need to use a fully qualified path including the ".cshtml" extension or even include the full path such as "~/DesktopModules/MVC/MyModule/Views/N_Notary/Index2.cshtml".

          Hope this helps





          New Around Here





            Thank you for answering my questions.

            yes this is work for me:

            Html.RenderPartial("~/DesktopModules/MVC/Notary/Views/Home/Index2.cshtml");

            ---------------------------

            Another way to useAnother way to useAnother way to useanother way to do that instead of coding in view is using controller like this(based on your answer):

                    var defaultStartAction = ModuleContext.Configuration.ModuleSettings.GetValueOrDefault("NotaryStartAction", "");
                        var defaultStartController = ModuleContext.Configuration.ModuleSettings.GetValueOrDefault("NotaryStartController", "");
                        if (!string.IsNullOrEmpty(defaultStartAction) && !string.IsNullOrEmpty(defaultStartController))
                        {
                            return Redirect(defaultStartAction, defaultStartController);
                        }
                        else
                        {
                            return Redirect("Index2", "Home");
                        }

            -------

            but the Redirect not worked correctly and i create class drived  from DnnController and add new Redirect method to it like this:

                public class MyDnnController : DnnController
                {

                    protected RedirectResult Redirect(string actionName, string controllerName, RouteValueDictionary routeValues = null)
                    {

                        if (routeValues == null)
                        {
                            routeValues = new RouteValueDictionary();
                        }
                        routeValues["controller"] = controllerName;
                        routeValues["action"] = actionName;
                        return Redirect(ModuleRoutingProvider.Instance().GenerateUrl(routeValues,this. ModuleContext));

                       
                    }

                }

            and then change inheritance of my Homecontroller to use "MyDnnController"

             public class HomeController : MyDnnController

            and this worked for me.

            thank you again.






            New Around Here





              No problem! Glad you got it working.

              Routing in the MVC modules sometimes is weird and it "should" work, but I sometimes need to use fully qualified view paths. The .cshtml file lookup should be able to find your .cshtml with just a partial.

              Another note I wanted to share for your Controller code. You really should look at the "Url.Action" capabilities as it makes Redirecting to other controllers super simple.

              • Url.Action - Generates the correct module specific route to a specific Controller/Action
              • RedirectToAction - Helper method that encapsualtes Url.Action 
                • RedirectToAction("Home", "Index") == Redirect(Url.Action("Home", "Index"))
              You are not authorized to post a reply.

              These Forums are dedicated to the discussion of DNN Platform.

              For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

              1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
              2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
              3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
              4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
              5. No Flaming or Trolling.
              6. No Profanity, Racism, or Prejudice.
              7. Site Moderators have the final word on approving / removing a thread or post or comment.
              8. English language posting only, please.

              Would you like to help us?

              Awesome! Simply post in the forums using the link below and we'll get you started.

              Get Involved