gpt4 book ai didi

匿名类型的 ASP.NET razor 三元表达式

转载 作者:行者123 更新时间:2023-12-04 22:31:05 27 4
gpt4 key购买 nike

我有以下代码:

<div class="col-sm-8 col-sm-pad ">
@Html.DropDownListFor(model => model.SelectedAssignToId, Model.AssignToListItems,
ViewBag.CanEditRequest ? new { @class = "form-control" } : new { @class = "form-control", @disabled = "disabled" })
</div>

但我收到以下错误:

error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'AnonymousType#1' and 'AnonymousType#2'



如何在 Razor 中的匿名类型上使用三元表达式?

最佳答案

您可以将三元表达式的一侧转换为 object :

@Html.DropDownListFor(model => model.SelectedAssignToId, Model.AssignToListItems,
ViewBag.CanEditRequest ? (object)new { @class = "form-control" } : new { @class = "form-control", @disabled = "disabled" })

或者尝试将三元表达式移动到匿名类型中,如下所示:
@Html.DropDownListFor(model => model.SelectedAssignToId, Model.AssignToListItems,
new { @class = "form-control", @disabled = ViewBag.CanEditRequest ? null : "disabled" })

关于匿名类型的 ASP.NET razor 三元表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25169871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com