将其他属性传递给EditorTemplate

时间:2015-01-13 15:03:33

标签: c# asp.net razor

如何将一些其他属性传递给EditorTemplate

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

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

1 个答案:

答案 0 :(得分:18)

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

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