How do I pass some additional properties to an EditorTemplate
?
I want to use it like this (kind of pseudo code):
@Html.EditorFor(m => m.ReturnFlight, new { additionalViewData = new { FlightType = FlightType.Return } })
@Html.EditorFor(m => m.OutboundFlight, new { additionalViewData = new { FlightType = FlightType.Outbound } })
FlightTemplate:
<h1>FLight @Model.FlightNumber</h1>
@if(FlightType == FlightType.Outbound)
{
// Display stuff for outbound flights
}
else if(FlightType == FlightType.Return)
{
// Display stuff for return flights
}
@Form.TextboxFor(m => m.Destination)
You pretty much have it already - you can pass additional view data in exactly this way, using this overload. You just need to use it in your editor template. Remember values in the ViewData
dictionary are also available in the dynamic ViewBag
object.
@Html.EditorFor(m => m.ReturnFlight, new { FlightType = FlightType.Return })
@Html.EditorFor(m => m.OutboundFlight, new { FlightType = FlightType.Outbound })
FlightTemplate
<h1>Flight @Model.FlightNumber</h1>
@if(ViewBag.FlightType == FlightType.Outbound)
{
// Display stuff for outbound flights
}
else if(ViewBag.FlightType == FlightType.Return)
{
// Display stuff for return flights
}
@Form.TextboxFor(m => m.Destination)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With