c# - DropDownListFor optionLabel null 给出 "call is ambiguous"

标签 c# asp.net-mvc

出于好奇,因为它很容易变通:this documentation for DropDownListFor表示 optionLabel 可以是 null。但是,如果我尝试将 optionLabel 编译为 null,我会收到一个call is ambiguous 错误,因为还有一个方法采用 >objectnull 在同一位置,显然是有歧义的。所以是 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/

相关文章:

c# - 重复事件——使用 DateTime?

c# - 直到运行时才知道属性的匿名类型

c# - 折叠行时动态调整数据网格列的大小

asp.net - 带有自定义 slugs 的 .NET MVC-4 路由

c# - ASP.NET MVC 5 表单验证

asp.net-mvc - 如何在 ASP.NET MVC 中为具有多个字段的 View 实现 "create" Controller 操作

c# - 从大文本文件中读取 unicode 字符

c# - 在 Entity Framework 中查询今天的记录

jquery - Controller 的 Json() 方法不返回数组

javascript - EditorTemplate 中的 Bootstrap 3 DateTimePicker