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.
Inherited Members
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
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 SourceDynamicList()
Initializes a new instance of the DynamicList<TViewModel, TOptions> class.
Declaration
public DynamicList()
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 SourceContainerId
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. |
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. |
Dictionary
Gets or sets the internal dictionary that is used to store
Declaration
protected Dictionary<string, TOptions> Dictionary { get; set; }
Property Value
Type | Description |
---|---|
Dictionary<String, TOptions> |
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 |
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. |
Item[String]
Gets the
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 |
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>. |
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>. |
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 SourceAdd(TViewModel)
Adds the specified
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 |
Add(TViewModel, Action<TOptions>)
Adds the specified
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 |
Returns
Type | Description |
---|---|
TOptions |
Add(TOptions)
Adds the specified
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.
AddRange(IEnumerable<TViewModel>, Func<TViewModel, TOptions>)
Adds multiple elements to this list. Note that, when added, the list
may change properties of the
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 |
AddRange(IEnumerable<TOptions>)
Adds multiple elements to this list. Note that, when added, the list
may change properties of the
Declaration
public void AddRange(IEnumerable<TOptions> options)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TOptions> | options | The elements to be added to this list. |
Clear()
Removes all items from this list.
Declaration
public void Clear()
Contains(TOptions)
Determines whether the this list contains a specific value. Note that only
the Index property of the provided
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 |
CopyTo(TOptions[], Int32)
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 |
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. |
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. |
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 |
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 |
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. |
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 |
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 SourceIDynamicList.Item[String]
Gets the
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.
IDynamicList<TOptions>.Item[String]
Gets the
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
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. |