出于好奇,因为它很容易变通:this documentation for DropDownListFor
表示 optionLabel
可以是 null
。但是,如果我尝试将 optionLabel
编译为 null
,我会收到一个call is ambiguous 错误,因为还有一个方法采用 >object
和 null
在同一位置,显然是有歧义的。所以是 a) 文档错误 b) 有一种方法可以编译和指定我的意思 c) 文档意味着不同的方法,其中 optionLabel
可以是 null
而不会导致调用不明确错误?
编辑:
我现在意识到,当然文档意味着 optionLabel
作为字符串变量传递的情况,总是传递 null
是没有意义的。例如:
string optionLabel = null;
// Possibly do something with optionLabel
@Html.DropDownListFor(x => x.Name, selectList, optionLabel)
最佳答案
文档有误。你不能:
@Html.DropDownListFor(x => x.Value, Model.Values, null)
因为这与 following overload
不明确.
或者,如果您坚持要传递 null,您可以使用命名参数来消除重载的歧义:
@Html.DropDownListFor(x => x.Name, new SelectList(null), optionLabel: null)
我个人经常使用命名参数来提高代码的可读性,因为 Microsoft 为 HTML 助手编写了无数重载。
关于c# - DropDownListFor optionLabel null 给出 "call is ambiguous",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113564/