4

当我尝试使用以下两个属性绑定模型时,在模型绑定期间出现错误:

    private IEnumerable<Claimant> _drivers;
    public IEnumerable<Claimant> Drivers
    {
        get
        {
            return _drivers ?? Enumerable.Empty<Claimant>();
        }
        set
        {
            _drivers = value;
        }
    }

    private IEnumerable<Property> _vehicles;
    public IEnumerable<Property> Vehicles
    {
        get
        {
            return _vehicles ?? Enumerable.Empty<Property>();
        }
        set
        {
            _vehicles = value;
        }
    }

错误:

System.Reflection.TargetInvocationException was unhandled by user code
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       <snip>
  InnerException: System.NotSupportedException
       Message=Collection is read-only.
       Source=mscorlib
       StackTrace:
            at System.SZArrayHelper.Clear[T]()
            at System.Web.Mvc.DefaultModelBinder.CollectionHelpers
                         .ReplaceCollectionImpl[T](ICollection`1 collection, IEnumerable newContents)
       InnerException: 

如果我将属性更改为基本自动属性:

    public IEnumerable<Claimant> Drivers { get; set; }
    public IEnumerable<Property> Vehicles { get; set; }

一切正常。

当设置器与自动属性设置器相同时,为什么模型绑定会出现问题?

编辑- 阅读默认模型绑定器源最终会导致您看到这一点,第一行是Clear()针对属性调用的,所以当我返回时,Empty<T>它显然不起作用。

private static void ReplaceCollectionImpl<T>(ICollection<T> collection, IEnumerable newContents) 
{
    collection.Clear();
    if (newContents != null) 
    {
        foreach (object item in newContents) 
        {
            // if the item was not a T, some conversion failed. the error message will be propagated,
            // but in the meanwhile we need to make a placeholder element in the array.
            T castItem = (item is T) ? (T)item : default(T);
            collection.Add(castItem);
        }
    }
}
4

4 回答 4

6

试试这样:

get 
{
    return _drivers ?? new List<Claimant>();
}
于 2012-06-18T15:06:52.177 回答
5

IIRCEnumerable.Empty<T>是一个静态的只读枚举,用于将一个空的与存储无关的枚举传递给方法。它并不意味着用作空集合的“起点”。这可能就是您收到错误的原因。

选择一种存储机制(例如List<T>)并将其用作支持字段的类型。然后你可以初始化它

  1. 在类定义中,
  2. 在构造函数中,或
  3. 首先得到:

例子:

private List<Claimant> _drivers = new List<Claimamt>();  // Option 1

public MyModel()
{
    _drivers = new List<Claimant>();   // Option 2
}

public IEnumerable<Claimant> Drivers
{
    get
    {
        return _drivers ?? (_drivers = new List<Claimant>()); // Option 3
    }
    set
    {
        _drivers = value;
    }
}
于 2012-06-18T15:22:42.670 回答
1

在自定义 getter 中,我通常会看到用于在返回时设置私有支持字段的空合并运算符 (??):

private IEnumerable<Claimant> _drivers;
public IEnumerable<Claimant> Drivers
{
    get
    {
        return _drivers ?? (_drivers = Enumerable.Empty<Claimant>());
    }
    set
    {
        _drivers = value;
    }
}

您还可以尝试将默认值设置为空数组:

(_drivers = new Claimant[]{})
于 2012-06-18T15:07:59.133 回答
1

找出导致问题的确切属性名称和类型:

public class TestBinder: DefaultModelBinder
{
    public override object BindModel(
            ControllerContext controllerContext,
            ModelBindingContext bindingContext)
    {
        Debug.WriteLine(bindingContext.ModelName);
        Debug.WriteLine(bindingContext.ModelType.ToString());

        //HOW TO USE: Look at your Output for the most recently output ModelName and Type to determine where there problem lies

        return base.BindModel(controllerContext, bindingContext);
    }
}

将此活页夹设置为默认值:

ModelBinders.Binders.DefaultBinder = new TestBinder();
于 2016-02-13T06:50:22.387 回答