2

私は次のタイプ階層を持っていますClientIndexModel

public class ViewModel
{
    public virtual IDictionary<string, SelectList> SelectListDictionary
    {
        get
        {
            var props = GetType().GetProperties().Where(p => p.PropertyType == typeof(SelectList));
            return props.ToDictionary(prop => prop.Name, prop => (SelectList)prop.GetValue(this, null));
        }
    }
}
public class IndexModel<TIndexItem, TEntity> : ViewModel where TIndexItem : ViewModel where TEntity : new()
{
    public List<TIndexItem> Items { get; private set; }
}
public class ClientIndexModel: IndexModel<ClientIndexItem, Client>
{
}

ClientIndexModel次のように、ApiControllerをインスタンス化して返します。

public ClientIndexModel Get()
{
    var model = new ClientIndexModel();
    return model;
}

行にmodelブレークポイントを設定して検査すると、プロパティは存在し、カウントは0です。ただし、このアクションから返されるJSONにはプロパティのみがあり、プロパティはありません。なぜこれができるのでしょうか?return model;ItemsSelectListDictionaryItems

4

1 に答える 1

2

あなたのItems財産にはプライベートセッターがいます。プライベートセッターを持つプロパティは、値を外部から変更できないため、逆シリアル化できないため、シリアル化しても意味がないため、意図的にシリアル化から除外されています。したがって、(プロパティに対して行ったように)セッターを完全に削除するか、SelectListDictionaryパブリックにするか、プライベートセッターでプロパティをシリアル化できるカスタムシリアライザーを使用してカスタムフォーマッターを作成する必要があります。

于 2012-04-24T17:17:35.477 に答える