Reputation: 1042
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)
Upvotes: 20
Views: 5401
Reputation: 16609
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)
Upvotes: 32