• Introduction
  • API Documentation
  • Sample Apps
  • GitHub

    Show / Hide Table of Contents
    • DynamicVML
      • AddNewDynamicItem
      • DynamicList<TViewModel, TOptions>
      • DynamicList<TViewModel>
      • DynamicListItem<TViewModel>
      • IDynamicList
      • IDynamicList<TValue>
      • IDynamicListItem
      • IDynamicListItem<TViewModel>
      • ListRenderMode
      • NewItemMethod
    • DynamicVML.Extensions
      • ControllerExtensions
      • EditorExtensions
      • EnumerableExtensions
      • ViewDataExtensions
    • DynamicVML.Internals
      • Constants
      • ItemDisplayParameters
      • ItemEditorParameters
      • ItemParameters
      • ListDisplayParameters
      • ListEditorParameters
      • ListParameters
      • Parameters
    • DynamicVML.Options
      • DynamicListAttribute
      • DynamicListDisplayOptions
      • DynamicListEditorOptions
      • DynamicListOptions

    Class DynamicList<TViewModel, TOptions>

    Represents a list of view model objects that can be added and removed from a form through Ajax. This class can be used to create lists of view models that contain custom options for your items.

    Inheritance
    Object
    DynamicList<TViewModel, TOptions>
    DynamicList<TViewModel>
    Implements
    ICollection<TOptions>
    IEnumerable<TOptions>
    IEnumerable
    IDynamicList<TOptions>
    IDynamicList
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: DynamicVML
    Assembly: DynamicVML.dll
    Syntax
    public class DynamicList<TViewModel, TOptions> : ICollection<TOptions>, IEnumerable<TOptions>, IEnumerable, IDynamicList<TOptions>, IDynamicList where TViewModel : class where TOptions : DynamicListItem<TViewModel>, new()
    Type Parameters
    Name Description
    TViewModel

    The type of the view model.

    TOptions

    The options class type containing the additional options you might want to specify, e.g., "Title", or "Subtitle".

    Remarks

    This class implements ICollection<T>, ICollection<T>, IDynamicList<TValue>, and IDynamicList.

    This class represents a list of HTML elements, but is actually implemented as a Dictionary<TKey,TValue> of unique div HTML element IDs and their associated objects and the ViewModel contained inside them.

    Note

    If you see yourself implementing many properties in your view model using this class, you might notice that your properties' type names will start to look a bit too long. In that case, you can subclass DynamicList<TViewModel, TOptions> and create a type that either always implements your custom options, or always use your desired view model. You can then use this subclass in as the property type of your view model collections instead of DynamicList<TViewModel, TOptions>. This should reduce the length of your property names significantly.

    Constructors

    | Improve this Doc View Source

    DynamicList()

    Initializes a new instance of the DynamicList<TViewModel, TOptions> class.

    Declaration
    public DynamicList()
    | Improve this Doc View Source

    DynamicList(String)

    Initializes a new instance of the DynamicList<TViewModel, TOptions> class. This constructor overload is only used when creating a new item to be added to an existing form in an HTML page.

    Declaration
    public DynamicList(string containerId)
    Parameters
    Type Name Description
    String containerId

    The ID of the HTML div element to which the contents of this list should be appended to.

    Properties

    | Improve this Doc View Source

    ContainerId

    The HTML div ID associated with this list. This ID is generated automatically using CreateId() and is guaranteed to be unique.

    Declaration
    public string ContainerId { get; set; }
    Property Value
    Type Description
    String

    The identifier for the HTML div element that contains the representation of this list in an HTML form.

    | Improve this Doc View Source

    Count

    Gets the number of elements contained in this list.

    Declaration
    public int Count { get; }
    Property Value
    Type Description
    Int32

    The number of elements in this list.

    | Improve this Doc View Source

    Dictionary

    Gets or sets the internal dictionary that is used to store by their HTML div id key.

    Declaration
    protected Dictionary<string, TOptions> Dictionary { get; set; }
    Property Value
    Type Description
    Dictionary<String, TOptions>
    | Improve this Doc View Source

    Index

    This property is required to help IModelBinder during runtime and does not have to be set to anything when creating the list.

    Declaration
    public string Index { get; set; }
    Property Value
    Type Description
    String
    | Improve this Doc View Source

    IsReadOnly

    Gets a value indicating whether this ICollection<T> is read-only. In the case of a DynamicList<TViewModel, TOptions>, it should always return false.

    Declaration
    public bool IsReadOnly { get; }
    Property Value
    Type Description
    Boolean

    Always false.

    | Improve this Doc View Source

    Item[String]

    Gets the object associated with the specified HTML div ID. Your view models are contained inside the object.

    Declaration
    public TOptions this[string id] { get; }
    Parameters
    Type Name Description
    String id

    The HTML div id for the element you want to retrieve.

    Property Value
    Type Description
    TOptions

    The object containing your ViewModel plus any additional options associated to it.

    | Improve this Doc View Source

    Keys

    Gets an enumerable that can be used to iterate through the identifiers of the div HTML elements used by your view models when rendered in a form.

    Declaration
    public IEnumerable<string> Keys { get; }
    Property Value
    Type Description
    IEnumerable<String>

    The keys, represented as an IEnumerable<T>.

    | Improve this Doc View Source

    Options

    Gets an enumerable that can be used to iterate through the Option objects associated with each of your ViewModel objects contained in this list. An Options object contains at least a property called "Index" which is necessary for ASP.NET's IModelBinder to work.

    Declaration
    public IEnumerable<TOptions> Options { get; }
    Property Value
    Type Description
    IEnumerable<TOptions>

    The view models, represented as an IEnumerable<T>.

    | Improve this Doc View Source

    ViewModels

    Gets an enumerable that can be used to iterate through your ViewModel objects that may be contained in this list.

    Declaration
    public IEnumerable<TViewModel> ViewModels { get; }
    Property Value
    Type Description
    IEnumerable<TViewModel>

    The view models, represented as an IEnumerable<T>.

    Methods

    | Improve this Doc View Source

    Add(TViewModel)

    Adds the specified object to the list. The list will automatically create a object to wrap it and provide it with an unique ID.

    Declaration
    public TOptions Add(TViewModel viewModel)
    Parameters
    Type Name Description
    TViewModel viewModel

    The view model object to be added to this list.

    Returns
    Type Description
    TOptions
    | Improve this Doc View Source

    Add(TViewModel, Action<TOptions>)

    Adds the specified object to the list. The list will automatically create a object to wrap it and provide it with an unique ID.

    Declaration
    public TOptions Add(TViewModel viewModel, Action<TOptions> options)
    Parameters
    Type Name Description
    TViewModel viewModel

    The view model object to be added to this list.

    Action<TOptions> options

    A method that can be used to customize the TOptions object that will be used to wrap the viewModel before it gets added to the list. This can be used to specify custom IDs for list items.

    Returns
    Type Description
    TOptions
    | Improve this Doc View Source

    Add(TOptions)

    Adds the specified object to the list. The Index property of your options object may be changed by this method upon insertion.

    Declaration
    public void Add(TOptions options)
    Parameters
    Type Name Description
    TOptions options

    The options object to be added to this list.

    Remarks

    If an item with a duplicate key gets inserted to this list, it will replace the old one with this new instance. This is not the same behavior of a standard Dictionary<TKey,TValue> but is needed to avoid model binding errors since IModelBinder will call this method to reconstruct the DynamicList<TViewModel, TOptions> object from the user's HTTP request.

    | Improve this Doc View Source

    AddRange(IEnumerable<TViewModel>, Func<TViewModel, TOptions>)

    Adds multiple elements to this list. Note that, when added, the list may change properties of the object (but not of your view models).

    Declaration
    public void AddRange(IEnumerable<TViewModel> viewModels, Func<TViewModel, TOptions> options = null)
    Parameters
    Type Name Description
    IEnumerable<TViewModel> viewModels

    The view models to be added to this list.

    Func<TViewModel, TOptions> options

    A default creation function that can be used to specify options associated with each of your view models. This is optional.

    | Improve this Doc View Source

    AddRange(IEnumerable<TOptions>)

    Adds multiple elements to this list. Note that, when added, the list may change properties of the object (but not of your view models).

    Declaration
    public void AddRange(IEnumerable<TOptions> options)
    Parameters
    Type Name Description
    IEnumerable<TOptions> options

    The elements to be added to this list.

    | Improve this Doc View Source

    Clear()

    Removes all items from this list.

    Declaration
    public void Clear()
    | Improve this Doc View Source

    Contains(TOptions)

    Determines whether the this list contains a specific value. Note that only the Index property of the provided object will be used to determine whether the list contains a similar element or not. This method will not look into the actual contents of your options or view model.

    Declaration
    public bool Contains(TOptions item)
    Parameters
    Type Name Description
    TOptions item

    The object to locate in the list.

    Returns
    Type Description
    Boolean

    true if item is found in the list; otherwise, false.

    | Improve this Doc View Source

    CopyTo(TOptions[], Int32)

    Copies the elements of this list to an Array, starting at a particular Array index.

    Declaration
    public void CopyTo(TOptions[] array, int arrayIndex)
    Parameters
    Type Name Description
    TOptions[] array

    The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.

    Int32 arrayIndex

    The zero-based index in array at which copying begins.

    | Improve this Doc View Source

    CreateId()

    This method creates an unique identified that can be used to identify HTML div elements in your form. Those IDs are needed in order to help ASP.NET's ASP.NET's IModelBinder bind the dynamic view models to your forms.

    Declaration
    protected static string CreateId()
    Returns
    Type Description
    String

    A string containing a GUID value in a HTML-friendly format.

    | Improve this Doc View Source

    GetEnumerator()

    Returns an enumerator that iterates through the collection.

    Declaration
    public IEnumerator<TOptions> GetEnumerator()
    Returns
    Type Description
    IEnumerator<TOptions>

    An enumerator that can be used to iterate through the collection.

    | Improve this Doc View Source

    Remove(TOptions)

    Removes the item with the same key specified in the Index property of the provided item.

    Declaration
    public bool Remove(TOptions item)
    Parameters
    Type Name Description
    TOptions item

    The object to remove from the list.

    Returns
    Type Description
    Boolean

    true if item was successfully removed from the list; otherwise, false. This method also returns false if item is not found in the original ICollection<T>.

    | Improve this Doc View Source

    ToModel<TModel>(Func<TViewModel, TModel>)

    Converts all view models stored inside this instance to their database model counterparts using the specified func.

    Declaration
    public IEnumerable<TModel> ToModel<TModel>(Func<TViewModel, TModel> func)
    Parameters
    Type Name Description
    Func<TViewModel, TModel> func

    A function that takes a and converts it to a .

    Returns
    Type Description
    IEnumerable<TModel>

    A IEnumerable<T> that can be materialized to an actual list/collection or just iterated over (e.g. using System.Linq.

    Type Parameters
    Name Description
    TModel

    The type for the models to convert to.

    | Improve this Doc View Source

    ToModel<TModel>(Func<TOptions, TModel>)

    Converts all view models stored inside this instance to their database model counterparts using the specified func. This overload can be used to take the options associated with each view model object into consideration during the conversion.

    Declaration
    public IEnumerable<TModel> ToModel<TModel>(Func<TOptions, TModel> func)
    Parameters
    Type Name Description
    Func<TOptions, TModel> func

    A function that takes a (which contains a ) and converts both to a .

    Returns
    Type Description
    IEnumerable<TModel>

    A IEnumerable<T> that can be materialized to an actual list/collection or just iterated over (e.g. using System.Linq.

    Type Parameters
    Name Description
    TModel

    The type for the models to convert to.

    Explicit Interface Implementations

    | Improve this Doc View Source

    IDynamicList.Item[String]

    Gets the with the specified identifier as an IDynamicListItem object.

    Declaration
    IDynamicListItem IDynamicList.this[string id] { get; }
    Parameters
    Type Name Description
    String id
    Returns
    Type Description
    IDynamicListItem
    Remarks

    This is an explicit interface implementation which is only available when interacting with this list through the IDynamicList interface. Normally, this should only be the case when accessing the list from a view.

    | Improve this Doc View Source

    IDynamicList<TOptions>.Item[String]

    Gets the with the specified identifier.

    Declaration
    TOptions IDynamicList<TOptions>.this[string id] { get; }
    Parameters
    Type Name Description
    String id
    Returns
    Type Description
    TOptions
    Remarks

    This is an explicit interface implementation which is only available when interacting with this list through the interface. Normally, this should only be the case when accessing the list from a view.

    | Improve this Doc View Source

    IEnumerable.GetEnumerator()

    Returns an enumerator that iterates through a collection.

    Declaration
    IEnumerator IEnumerable.GetEnumerator()
    Returns
    Type Description
    IEnumerator

    An IEnumerator object that can be used to iterate through the collection.

    Implements

    System.Collections.Generic.ICollection<T>
    System.Collections.Generic.IEnumerable<T>
    System.Collections.IEnumerable
    IDynamicList<TValue>
    IDynamicList

    Extension Methods

    EnumerableExtensions.ToDynamicList<TViewModel, TOptions>(TViewModel, String, TOptions)
    EnumerableExtensions.ToDynamicList<TViewModel>(TViewModel, String)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions>(TViewModel, String, Func<TViewModel, TOptions>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions, TModel>(TModel, String, Func<TModel, TViewModel>, Func<TModel, TOptions>)
    EnumerableExtensions.ToDynamicList<TViewModel, TModel>(TModel, String, Func<TModel, TViewModel>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions, TModel>(TModel, String, Func<TModel, TOptions>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions>(IEnumerable<TViewModel>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions>(IEnumerable<TViewModel>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions>(IEnumerable<TViewModel>, Func<TViewModel, TOptions>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions, TModel>(IEnumerable<TModel>, Func<TModel, TViewModel>, Func<TModel, TOptions>)
    EnumerableExtensions.ToDynamicList<TViewModel, TModel>(IEnumerable<TModel>, Func<TModel, TViewModel>)
    EnumerableExtensions.ToDynamicList<TViewModel, TOptions, TModel>(IEnumerable<TModel>, Func<TModel, TOptions>)

    See Also

    DynamicList<TViewModel>
    ICollection<T>
    IDynamicList<TValue>
    IDynamicList
    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 César Roberto de Souza - All files are available under the MIT license except the logo, which comes from FontAwesome.