0

I'm working on updating a project where many different views share the same layout, with only minor differences in content between them.

The basic layout looks like this:

@Html.Partial("_HeadingBar")

<div class="panel grid_4">

    <div class="panel-header">
        <span>@ViewBag.SmallHeader</span>
    </div>

    <div class="panel-body">
        <div class="panel-content">
            @* Welcome.cshtml:
            <p>Please use the navigation bar on the side of the site to utilize the features of the site.</p> *@

            @* Login.cshtml:
            @using (Html.BeginForm())
            {
                if (!ViewData.ModelState.IsValid) ...

                <div class="form-row-inline">
                    @Html.LabelFor(m => m.UserName)
                    @Html.TextBoxFor(m => m.UserName)
                    @Html.ValidationMessageFor(m => m.UserName)
                </div>
                ...
                <button type="submit" class="btn btn-success"><i class="icon-lock"></i> Sign In</button>
            } *@

            @* ChangePassword.cshtml:
            @using(Html.BeginForm())
            {
                ...
                @Html.LabelFor(m => m.NewPassword)
                @Html.PasswordFor(m => m.NewPassword)
                @Html.ValidationMessageFor(m => m.NewPassword)
                ...
            } *@


        </div>
    </div>
</div>

My thought was to strip each view down to the unique content and make it a partial view, and make the shared part into a regular view.

But I'm not sure that's the correct way to do this. Adding Html.RenderPartial would result in a second call to the server, and would require additional Actions to be added to the Controller.

It seems like I'm looking for a layout, but the site already uses a layout.

What's the correct/best-practice way to handle this?

CarenRose
  • 1,266
  • 1
  • 12
  • 24

1 Answers1

0

I think youre going the right way about it. Personally i split large views into smaller features also means controllers etc. are easier to read and maintain.

All helper methods including HtmlRenderPartial are rendered serverside upon receiving a request unless cached. so there shouldnt be an additional call to the server.

There are several html helper methods you can use

Partials without controllers

@Html.Partial("~/Views/Shared/Partials/SomePartialViewWithNoModel.cshtml")
@Html.Partial("~/Views/Shared/Partials/SomePartialViewWithModel.cshtml", Model)

Partials with controllers

@Html.Action("RenderPartial", "ControllerWithNoModelMethod")
@Html.Action("RenderPartial", "ControllerWithModelMethod", new { content = Model })
@Html.RenderAction("RenderPartial", "ControllerWithNoModelMethod")
@Html.RenderAction("RenderPartial", "ControllerWithModelMethod", new { content = Model })

The difference between Action and RenderAction is that Action returns the result as a string and RenderAction returns the result inline. Read more

Greg
  • 129
  • 11
  • Not all of the partials will have a model, let alone the same type as their model, though. – CarenRose Apr 06 '20 at 17:14
  • Apologies i may have misunderstood some of the question, will these potential partial views be used more than once? what are the similarities? Theres other helpers for things such as sections, can define a section in a layout and populate it individually per view. [read more](https://stackoverflow.com/questions/13089489/asp-net-mvc-what-is-the-purpose-of-section) Also you can nest layouts if thats what youre after [read more](https://www.mikesdotnetting.com/article/164/nested-layout-pages-with-razor) – Greg Apr 06 '20 at 17:38