将其他属性传递给EditorTemplate

Ste*_*mid 14 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 } })
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

Rhu*_*orl 23

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

@Html.EditorFor(m => m.ReturnFlight, new { FlightType = FlightType.Return })
@Html.EditorFor(m => m.OutboundFlight, new { FlightType = FlightType.Outbound })
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)