dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

Allow sections to be defined inside Views and Partial Views and let them have Models #43713

Open AlexZeitler opened 2 years ago

AlexZeitler commented 2 years ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

If you're creating a View which itself consists of multiple partial views you can add more structure to your views.

However, splitting the view into multiple (small) files can become difficult to manage and to reason about.

Describe the solution you'd like

I would like to use it in a way using and extending the existing concept of sections and allow them to have model parameters like Views and Partial Views already have and being able to define them in views and partial views as well.

This is what it could look like:

/Views/Contacts/Contact.cshtml:

@model IReadOnlyCollection<ContactViewModel>

@{
  ViewBag.Title = "Contacts";
  Layout = "_Layout";
}

@section Details(ContactViewModel contact) {
  <li>
    <span>@contact.Name</span>
    @await Html.SectionAsync("ArchiveView", contact)
  </li>
}

@section ArchiveView(ContactViewModel contact) {
<div>
  @if (contact.IsArchived)
  {
    <button>Unarchive</button>
  }
  else
  {
    <button>Archive</button>
  }
</div>
}

<h2>Contacts</h2>
<div class="w-80">
  <div class="mt-6 flow-root">
    <ul class="-my-5 divide-y divide-gray-200"
        role="list">
      @foreach (var contact in Model)
      {
        @await Html.SectionAsync("Details", contact)
      }
    </ul>
  </div>
</div>

From a controller perspective, I should be able to call them also, somewhat similar to partial views:

public async Task<IActionResult> ContactDetails(Guid id) {
  var contact = await LoadContact(id);
  return Section("Contact.cshtml", "Details", new ContactViewModel(contact));
}

Additional context

This is inspired by the idea of fragments in this post.

HTML over the wire (Hotwire, Turbo, HTMX etc.) gains more popularity and having this at hand in ASP.NET Core can make it a first class choice for those paradigms.

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

anpin commented 2 years ago

This would really make a substantial difference for htmx. Currently one have to put every single bit into a separate component which often becomes cumbersome.

AlexZeitler commented 1 year ago

This would also be helpful if you're using vertical slice architecture without HTMX as shown in this video:

Tired of Layers? Vertical Slice Architecture to the rescue!