The next example passes in a word to the sub-template, so that can change what it does based on the variable.
For the Razor14
we will use the parameters on DynamicModel
(see code below)
The same Example for RazorPro
(new v16). The called template will get the parameters on MyModel
(see code below)
-
-
⬇️ Result | Source ➡️
Hello, this is the first line
Second line!
I'm last!
@inherits Custom.Hybrid.RazorTyped
@Html.Partial("Box Typed.cshtml", new { Label = "Hello, this is the first line" })
@Html.Partial("Box Typed.cshtml", new { Label = "Second line!", Color = "red" })
@Html.Partial("Box Typed.cshtml", new { Label = "I'm last!", Color = "blue" })
Source Code of Box Typed.cshtml
@inherits Custom.Hybrid.RazorTyped
@{
// pick up parameters which were passed in
var label = MyModel.String("Label");
var color = MyModel.String("Color", fallback: "black"); // default to "black" if not provided
}
<div style="border: solid 2px @color">
@label
</div>