locked
Share a partial view among different Areas in MVC RRS feed

  • Question

  • User-2084339468 posted

    I am working on a moderately big MVC project. This project consists of different Areas. There are separate Views and models for each Area. I am having an issue with controlling a part in my Master Layout page.
    There is a Partial View inside every Area which does the same job. A specific model that is used in the partial view is the same.
    So despite having different Partial Views inside every area, I was trying to have a Common Partial View that can be used by every Area. But I can't come up with a suitable solution.
    Can anybody help me with this? Where should I put my Common Partial View so that every View in every Area can use it?

    Sunday, July 12, 2020 8:11 PM

All replies

  • User1686398519 posted

    Hi mayukh_sen.

    You can achieve your needs by modifying _ViewStart.cshtml. Below I will explain how to achieve it.

    • When you create a View in Area, a file called _ViewStart.cshtml is automatically created. If you haven't modified it, by default, it looks like this.
      • @{ Layout = "~/Areas/Area1/Views/Shared/_Layout.cshtml"; }
    • I modified the _ViewStart.cshtml file of each Area to look like this.
      • @{Layout = "~/Views/Shared/_Layout.cshtml";}
    • I created a partial view called _sharedPartialView.cshtml in the "Views/Shared" directory and used it in the _Layout.cshtml file.
    • Note:
      • For testing, I created a Model called Test in the Models folder which is under the root directory.
      • Because there are Controller and View with the same name in my project, I also modified my Route configuration.
      • I created two Areas,since they are basically the same, the following example only shows one of them.

    Model

        public class Test
        {
            public int Id { get; set; }
            public string content { get; set; }
        }

    _sharedPartialView.cshtml(In the Views/Shared directory)

    <p>This is _sharedPartialView</p>
    @model WebApplication13.Models.Test
    @Html.TextBoxFor(m => m.Id)
    @Html.TextBoxFor(m => m.content)

    _Layout.cshtml(In the Views/Shared directory)

        <div class="container body-content">
            @RenderBody()
            @Html.Partial("~/Views/Shared/_sharedPartialView.cshtml")
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
            </footer>
        </div>

    RouteConfig

            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    new string[] { "Webapplication13.Controllers" }
                );
            }

    _ViewStart.cshtml(In the Areas/Area1/Views directory)

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    Area1AreaRegistration

            public override void RegisterArea(AreaRegistrationContext context) 
            {
                context.MapRoute(
                    "Area1_default",
                    "Area1/{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional },
                    new string[] { "Webapplication13.Areas.Area1.Controllers" }
                );
            }

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, July 13, 2020 2:45 AM