Skip to content

Instantly share code, notes, and snippets.

@zplume
Created October 7, 2015 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zplume/919860b34b1bf10cb4da to your computer and use it in GitHub Desktop.
Save zplume/919860b34b1bf10cb4da to your computer and use it in GitHub Desktop.
Creating a component in MVC 5 to avoid lots of repetition in your Views (DRY!)
<!--Views\Shared\User.cshtml-->
@model MyNamespace.ViewModels.User
<div>@Model.Name</div>
<!--
This is a very basic example, obviously you'd have more complex nested HTML
and more properties in use for it to be worth using a component like this
-->
<!--Views\MainController\MainView.cshtml-->
@model MyNamespace.ViewModels.MainModel
<h2>Team 1</h2>
@Html.Partial("User", Model.User1)
@Html.Partial("User", Model.User2)
<h2>Team 2</h2>
@Html.Partial("User", Model.User3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment