Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass additional properties to an EditorTemplate

Tags:

c#

asp.net

razor

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)
like image 696
Stefan Schmid Avatar asked Jan 13 '15 15:01

Stefan Schmid


1 Answers

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)
like image 190
Rhumborl Avatar answered Sep 28 '22 09:09

Rhumborl