Setup with MVC Controllers
Rizzy transforms your traditional ASP.NET MVC applications by integrating Razor Components into your controllers. This powerful integration enables you to build modern, interactive UIs while still leveraging the familiar MVC architecture. In this guide, you will learn how to render Razor Component views and partial views, combine multiple render fragments, and handle form data within your components.
Getting Started
To begin, update your MVC controllers so that they inherit from either RzController or RzControllerWithViews. This allows Rizzy to automatically cascade ModelState and pass any necessary data to your Razor Components.
For example, create a controller that renders Razor Component views:
// HomeController.cs - Full Component View
public class HomeController : RzController
{
public IResult Index()
{
// Renders the HomeIndex component as a full view.
return View<HomeIndex>();
}
}If your application needs to return both Razor Component views and traditional MVC views, use:
// HomeController.cs - Mixed Views
public class HomeController : RzControllerWithViews
{
// Controller actions can return both Razor Component views and standard MVC views.
}Rendering Views and Partial Views
Rizzy extends your controllers with methods to render Razor Components as full views or partial views. Here are the key methods provided by RzController:
View<TComponent>(object? data = null)
Renders the specified Razor Component as a full view. Pass parameters using an anonymous object.View<TComponent>(Dictionary<string, object?> data)
Accepts a dictionary of parameters, which is useful when passing multiple pieces of data.PartialView<TComponent>(object? data = null)
Renders the specified Razor Component as a partial view without the outer layout. Ideal for widgets or reusable UI segments.PartialView<TComponent>(Dictionary<string, object?> data)
Works like the full view overload but accepts a dictionary.
Passing Parameters
Rizzy provides two robust, type-safe approaches for passing parameters to your views:
1. The Fluent Builder
You can use the RizzyComponentParameterBuilder to pass parameters to your component fluently. This method ensures compile-time safety and validates required parameters:
public IResult WeatherInfo()
{
return View<Weather>(parameters =>
parameters.Add(weather => weather.ZipCode, 12345));
}2. The .Params() Source Generator
By decorating your Blazor component with the [RizzyParameterize] attribute, a source generator automatically creates a strongly-typed Params method for your component. This is the highly recommended approach as it creates an elegant API surface directly tied to your component properties.
@* Weather.razor *@
@attribute [RizzyParameterize]
@code {
[Parameter] public int? ZipCode { get; set; } = null;
}You can then pass parameters cleanly in your controller:
public IResult WeatherInfo()
{
// Utilizing the auto-generated Params method
return View<Weather>(Weather.Params(zipCode: 12345));
}Rendering Multiple Render Fragments
Rizzy also supports rendering multiple render fragments in a single partial view. This feature is especially useful when you want to compose a page from several independent UI sections without wrapping them in one large component.
Use the overload that accepts an array of RenderFragment objects:
MultiFragments.razor
@code {
// A simple header fragment defined with inline markup.
public static RenderFragment Header => @<header>This is the header section.</header>;
// A main content fragment that accepts dynamic content as a parameter.
public static RenderFragment MainContent(string content) =>
@<main>
@content
</main>;
// A footer fragment defined with inline markup.
public static RenderFragment Footer => @<footer>This is the footer section.</footer>;
}Controller Method
public IResult MultiFragmentView()
{
// Render all fragments together as a composite partial view.
return PartialView(
MultiFragments.Header,
MultiFragments.MainContent("This is the main content area."),
MultiFragments.Footer
);
}In this example, the header, content, and footer fragments are rendered sequentially to build a complete composite view, making your UI modular and maintainable.
Handling Form Data
RzController passes the controller’s ModelState to every view and partial view (unless you pass a different ModelStateDictionary), so model-binding and validation errors from your action are available to your Razor components.
To show them, place <RzModelStateProvider /> inside the EditForm. It copies the ModelState errors into the form’s EditContext, so RzValidationMessageBase and RzValidationSummaryBase display them next to each field. See Validation for the full flow, including answering with status 422.
Antiforgery in MVC
Protect posting actions with [ValidateAntiForgeryToken], and register MVC with AddControllersWithViews(): AddControllers() alone does not register the filter that attribute needs. rizzy.js sends the token in a header on every htmx request that isn’t a GET.
There are two approaches to working with form data in your components:
1. Passing the Model as a Parameter
In this approach, the controller passes the model explicitly to the view, and your component declares a property decorated with [Parameter]. This method is useful when you want to explicitly control the data being sent to your component.
Controller Example:
public IResult SubmitForm(Person person)
{
if (!ModelState.IsValid)
{
// Status 422; htmx 4 swaps it. ModelState is passed along automatically.
Response.Htmx().ValidationFailed();
return PartialView<FormComponent>(FormComponent.Params(model: person));
}
// Process valid data...
return View<SuccessComponent>();
}Razor Component Example:
@* FormComponent.razor *@
@attribute [RizzyParameterize]
<EditForm Model="@Model">
<RzModelStateProvider />
<!-- Form inputs go here -->
</EditForm>
@code {
[Parameter]
public Person? Model { get; set; }
}2. Binding the Model Directly from the Form
Alternatively, you can allow the form to supply the model data directly to your component by using [SupplyParameterFromForm]. In this scenario, you do not need to pass the model from your controller.
Note on [SupplyParameterFromForm] Integration
Rizzy aims to make integrating Blazor components into MVC as smooth as possible. To enable the convenient [SupplyParameterFromForm] attribute for binding data directly from forms in this context, Rizzy uses reflection to connect with internal ASP.NET Core services (like HttpContextFormDataProvider). It runs when any Rizzy view executes (controllers and minimal APIs alike), reads the form asynchronously, and fails with a clear error message if a future ASP.NET Core version changes those internals.
This technique bridges a gap where public APIs are not currently available, avoiding the need to replicate complex framework logic within Rizzy itself. While this integration is designed to be robust, it’s important to understand that it interfaces with non-public parts of the .NET framework. As such, significant changes in future .NET versions could impact this specific feature, potentially requiring adjustments in Rizzy to maintain compatibility. We are committed to monitoring this and ensuring Rizzy works smoothly across .NET updates.
Controller Example:
public IResult SubmitForm()
{
// No need to pass the model explicitly; it will be bound from the form.
return PartialView<FormComponent>();
}Razor Component Example:
@* FormComponent.razor using form binding *@
<EditForm Model="Person" FormName="person">
<RzModelStateProvider />
<!-- Form inputs go here -->
</EditForm>
@code {
[SupplyParameterFromForm(FormName = "person")]
public Person? Person { get; set; }
protected override void OnInitialized() => Person ??= new();
}Both approaches make your form data available in your Razor component, with <RzModelStateProvider> showing any ModelState errors. Add <RzInitialValidator /> as well if the form should run its Data Annotations validation as soon as it renders.
Conclusion
By upgrading your MVC controllers to inherit from RzController or RzControllerWithViews, you unlock the ability to integrate Razor Components into your application seamlessly. Rizzy passes your controller’s ModelState to your views, supports both full and partial view rendering, and even allows you to combine multiple render fragments into a single composite view.
Whether you choose to pass your model explicitly using [Parameter] or bind it directly from the form with [SupplyParameterFromForm], Rizzy adapts to your workflow. With these capabilities at your fingertips, you can build rich, interactive web applications that blend the best of both MVC and modern component-based architectures.
Happy coding!