将附加属性传递给 EditorTemplate

Pass additional properties to an EditorTemplate

如何将一些额外的属性传递给 EditorTemplate

我想这样使用它(一种伪代码):

@Html.EditorFor(m => m.ReturnFlight, new { additionalViewData = new { FlightType = FlightType.Return } })
@Html.EditorFor(m => m.OutboundFlight, new { additionalViewData = new { FlightType = FlightType.Outbound } })

航班模板:

<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)

您几乎已经掌握了它 - 您可以使用 this overload 以这种方式传递额外的视图数据。您只需要在您的编辑器模板中使用它。请记住 ViewData 字典中的值在动态 ViewBag 对象中也可用。

@Html.EditorFor(m => m.ReturnFlight, new { FlightType = FlightType.Return })
@Html.EditorFor(m => m.OutboundFlight, new { FlightType = FlightType.Outbound })

飞行模板

<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)