Saturday, 5 September 2020

Bind dynamic layout on a single view using C# | ASP.Net

 

ASP.Net MVC is a popular platform to design dynamic website and CRM etc. One I was stuck in a situation when I want to use a common view to different privileged users. So I had to bind multiple layouts on a single view as per the related users menu or sidebar. This post will help you to bind dynamic layouts on a single view.

  public ActionResult Task()

        {

            CommonRepository ob = new CommonRepository();

            AdminDashboardModel data = new AdminDashboardModel();

            data = ob.GetOtherUsersDashbaord(Request.Cookies["UID"].Value);

         

            string Layout = "";

            switch (Request.Cookies["Type"].Value)

            {

                case "Designer":

                    Layout = "~/Views/Shared/_LayoutDesigner.cshtml";

                    break;

                case "IT":

                    Layout = "~/Views/Shared/_LayoutIT.cshtml";

                    break;

                case "Sales":

                    Layout = "~/Views/Shared/_LayoutSales.cshtml";

                    break;

                case "Coordinator":

                    Layout = "~/Views/Shared/_LayoutOperator.cshtml";

                    break;

                case "Admin":

                    Layout = "~/Views/Shared/_LayoutOperator.cshtml";

                    break;

 

            }

            return View("Task", Layout,data);

        }

in the Given example above layout is passing from the controller which is dynamically bound with a switch case . condition is applied as user type in my example.  


No comments:

Post a Comment