A demonstration project showcasing how to create reusable UI components in ASP.NET Core MVC using Partial Views. This project illustrates a component-based architecture pattern for building modular and maintainable web applications.
This project demonstrates the implementation of a card component system using ASP.NET Core MVC Partial Views. It shows how to create reusable UI components that can be easily customized and integrated throughout your application.
- Reusable Card Component: A flexible card component with customizable properties
- Component-Based Architecture: Organized structure for scalable UI components
- Bootstrap Integration: Responsive design with Bootstrap styling
- Model-Driven Components: Strongly-typed components using view models
MVC_Components/
├── Controllers/
│ └── HomeController.cs # Main controller with component demo
├── Models/
│ ├── CardViewModel.cs # View model for card component
│ └── ErrorViewModel.cs # Error handling view model
├── Views/
│ ├── Home/
│ │ ├── Index.cshtml # Demo page showing multiple cards
│ │ └── Privacy.cshtml # Privacy page
│ └── Shared/
│ ├── Components/
│ │ └── _Card.cshtml # Reusable card partial view
│ ├── _Layout.cshtml # Main layout template
│ └── _ValidationScriptsPartial.cshtml
├── wwwroot/
│ └── css/
│ └── site.css # Custom styles including card components
└── Program.cs # Application configuration
The CardViewModel
class defines the structure and properties of the card component:
public class CardViewModel
{
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string? Footer { get; set; }
public string? ImageUrl { get; set; }
public string? ActionUrl { get; set; }
public string? ActionText { get; set; }
public string CssClass { get; set; } = "card-default";
public bool ShowBorder { get; set; } = true;
public bool ShowShadow { get; set; } = true;
}
The card component (Views/Shared/Components/_Card.cshtml
) is a reusable partial view that renders:
- Optional image header
- Title and content sections
- Optional action button
- Optional footer
- Customizable styling and borders
To use the card component in your views:
<partial name="Components/_Card" model="cardViewModel" />
In the controller:
var card = new CardViewModel
{
Title = "Welcome Card",
Content = "This is a sample card component demonstrating reusable UI in MVC.",
Footer = "Last updated: " + DateTime.Now.ToString("MMM dd, yyyy"),
CssClass = "card-success",
ActionUrl = "/Home/Privacy",
ActionText = "Learn More"
};
The card component supports various CSS classes for different styles:
card-primary
- Blue accent bordercard-success
- Green accent bordercard-warning
- Yellow accent bordercard-danger
- Red accent bordercard-border
- Adds border stylingcard-shadow
- Adds drop shadow with hover effects
The component intelligently renders elements based on provided data:
- Images are only displayed if
ImageUrl
is provided - Action buttons appear only when both
ActionUrl
andActionText
are set - Footer section renders only if
Footer
content exists - Border and shadow effects can be toggled via boolean properties
- .NET 9.0 or later
- Visual Studio 2022 or Visual Studio Code
- Clone the repository
- Navigate to the project directory
- Run the following commands:
dotnet restore
dotnet build
dotnet run
- Open your browser and navigate to
https://localhost:5001
orhttp://localhost:5000
The demo page displays multiple card instances to showcase the component's flexibility. You can modify the card data in HomeController.cs
(lines 12-95) to experiment with different configurations.
- Reusability: Components can be used across multiple views and controllers
- Maintainability: Changes to component structure only need to be made in one place
- Consistency: Ensures uniform appearance and behavior across the application
- Flexibility: Easy to customize through view model properties
- Type Safety: Strongly-typed models prevent runtime errors
This project demonstrates:
- How to create partial views for reusable components
- Model-driven component architecture
- Conditional rendering in Razor views
- CSS organization for component styling
- Bootstrap integration with custom components
- MVC pattern implementation for component-based UI
To extend this project, consider:
- Adding more component types (buttons, modals, forms)
- Implementing component composition patterns
- Adding JavaScript functionality to components
- Creating a component library structure
- Implementing ViewComponents for more complex scenarios
- Framework: ASP.NET Core MVC (.NET 9.0)
- Frontend: Bootstrap 5, HTML5, CSS3
- Templating: Razor Views
- Architecture: Model-View-Controller (MVC)