gpt4 book ai didi

asp.net-mvc - ListBox for ArgumentNullException 参数名称 : source

转载 作者:行者123 更新时间:2023-12-04 12:12:39 25 4
gpt4 key购买 nike

设置:

我使用 MvcScaffolding 搭建了一个 Controller 。

对于属性 Model.IdCurrencyFrom,脚手架创建了一个 Html.DropDownListFor:

@Html.DropDownListFor(model => model.IdCurrencyFrom, 
((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.CurrencyName),
Value = option.CurrencyId.ToString(),
Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
}), "Choose...")

这适用于新记录或编辑现有记录。

问题:

只有 3 种货币,AR$、US$ 和 GB£。因此,我想要一个 ListBox,而不是下拉列表。

所以我把上面的改成了:
@Html.ListBoxFor(model => model.IdCurrencyFrom, 
((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.CurrencyName),
Value = option.CurrencyId.ToString(),
Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
}))

我现在得到一个 ArgumentNullException,参数名称:源,但仅在编辑现有记录时。创建新记录,这很好用。

问题:

怎么了?!

什么也没有变。切换回 DropDownListFor,一切正常。切换到 ListBox(而不是 ListBoxFor),我收到错误消息。

该模型不是空的(就像我说的,它与 DropDownListFor 一起工作得很好)......而且我已经没有想法了。

最佳答案

我检查了 HTML 助手的来源,这是一个有趣的练习。

TL; 博士;
问题是 ListBoxFor 用于多选,它需要一个可枚举的 Model 属性。您的 Model 属性( model.IdCurrencyFrom )不是可枚举的,这就是您收到异常的原因。

以下是我的发现:

  • ListBoxFor 方法将呈现 select带有 multiple="multiple" 的元素属性,总是。它被硬编码在 System.Web.Mvc.Html.SelectExtensions
    private static MvcHtmlString ListBoxHelper(HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes) {
    return SelectInternal(htmlHelper, null /* optionLabel */, name, selectList, true /* allowMultiple */, htmlAttributes);
    }

    所以也许您无论如何都不想允许用户使用多种货币...
  • 当此 ListBoxHelper 尝试从您的模型属性中获取默认值时,您的问题就开始了:
    object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string)); 

    它适用于 DropDownList 因为它将 false 传递给 allowMultiple打电话时SelectInternal .
    因为您的 ViewData.ModelState defaultValue 为空(因为之前没有在您的 Controller 中进行过验证)将是 null .然后defaultValue使用您的模型的默认值进行初始化(您的情况 model.IdCurrencyFromint 我猜)所以它将是 0 . :
    if (!usedViewData) {
    if (defaultValue == null) {
    defaultValue = htmlHelper.ViewData.Eval(fullName);
    }
    }

    我们正在接近异常:) 因为正如我提到的 ListBoxFor 只支持多选,所以它试图处理 defaultValueIEnumbrable :
    IEnumerable defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
    IEnumerable<string> values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture);

    在第二行中有你的 ArgumentException 因为 defaultValuesnull .
  • 因为它期望defaultValue是可枚举的,因为字符串是可枚举的。如果更改 model.IdCurrencyFrom 的类型至 string它会起作用。但是当然,您将在 UI 上进行多项选择,但您只会获得模型中的第一个选择。
  • 关于asp.net-mvc - ListBox for ArgumentNullException 参数名称 : source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7145464/

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