English 中文(简体)
ListBox for ArgumentNullException 参数名称:来源
原标题:ListBox for ArgumentNullException Parameter name: source

Setup:

我用MvcScaf双管齐下。

不动产,《示范法》。 IdCurrent 从那以后,手法产生了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...")

无论是新记录,还是对现有记录进行编辑,都处以罚款。

Problem:

仅有3种货币,即 AR、美元和英镑。 因此,我不想把名单降为名单Box。

因此,我把以上改为:

@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)
    }))

现在,我收到一份《阿古特努勒约》,参数名称:来源,但只是在编辑现有记录时。 制作新的记录,这项工作很出色。

Questions:

正在发生什么?

没有变化。 转回DownListFor,所有工作都罚款。 转至名单Box(而不是名单BoxFor),我有错误。

这个模式并不完全无效(如我说过的那样,它与DroopDownListFor公司合作,我摆脱了思想。

最佳回答

I ve checked the source of the HTML helpers, it was a fun exercise.

TL;DR; The problem is that ListBoxFor is for multiple selection and it expects an enumerable Model property. Your Model property (model.IdCurrencyFrom) is not an enumerable that s why you get the exception.

我的调查结果如下:

  1. 清单BoxFor方法将始终提供<条码>电子数据要素和<条码>多功能”特性。 硬编码见System.Web.Mvc.Html.SelectExtens

    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);
    }
    

    因此,或许你不想允许使用多种货币......

  2. 你的问题始于本名单BoxHelper试图从你的模式财产中获得违约价值:

    object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string)); 
    

    It works for DropDownList because it passes false to allowMultiple when calling SelectInternal.
    Because your ViewData.ModelState is empty (because there were no validation occurred in your controller before) the defaultValue will be null. Then defaultValue gets initialized with your model s default value (your case model.IdCurrencyFrom is int I guess) so it will be 0. :

    if (!usedViewData) {
            if (defaultValue == null) {
                defaultValue = htmlHelper.ViewData.Eval(fullName);
            } 
     }
    

    We are getting close to the exception :) Because as I mentioned ListBoxFor only support multiple selection, so it tries to handle defaultValue as IEnumbrable:

    IEnumerable defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
    IEnumerable<string> values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture); 
    

    在第二行中,“欢迎”,因为<代码>defaultValues为

  3. 由于它希望<代码>defaultValue能够计算,并且因为体内可计算。 如果您将<代码>model.IdCurrencyFrom的类型改为string,它将有效。 但是,当然,你将选择统一制度,但你只能选择你的模式。

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签