Rename ModelState (the type) -> ModelStateEntry

Fixes #3326
This commit is contained in:
Pranav K 2015-10-20 12:19:36 -07:00
parent 173f00fda7
commit 8b0c157296
19 changed files with 268 additions and 263 deletions

View File

@ -6,7 +6,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <summary>
/// An entry in a <see cref="ModelStateDictionary"/>.
/// </summary>
public class ModelState
public class ModelStateEntry
{
/// <summary>
/// Gets the raw value from the request associated with this entry.

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// Represents the state of an attempt to bind values from an HTTP Request to an action method, which includes
/// validation information.
/// </summary>
public class ModelStateDictionary : IDictionary<string, ModelState>
public class ModelStateDictionary : IDictionary<string, ModelStateEntry>
{
// Make sure to update the doc headers if this value is changed.
/// <summary>
@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
public static readonly int DefaultMaxAllowedErrors = 200;
private readonly Dictionary<string, ModelState> _innerDictionary;
private readonly Dictionary<string, ModelStateEntry> _innerDictionary;
private int _maxAllowedErrors;
/// <summary>
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
MaxAllowedErrors = maxAllowedErrors;
_innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);
_innerDictionary = new Dictionary<string, ModelStateEntry>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
@ -53,7 +53,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(dictionary));
}
_innerDictionary = new Dictionary<string, ModelState>(
_innerDictionary = new Dictionary<string, ModelStateEntry>(
dictionary,
StringComparer.OrdinalIgnoreCase);
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// the error message will be ignored and a <see cref="TooManyModelErrorsException"/> will be added.
/// </para>
/// <para>
/// Errors added via modifying <see cref="ModelState"/> directly do not count towards this limit.
/// Errors added via modifying <see cref="ModelStateEntry"/> directly do not count towards this limit.
/// </para>
/// </remarks>
public int MaxAllowedErrors
@ -122,7 +122,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// <inheritdoc />
public bool IsReadOnly
{
get { return ((ICollection<KeyValuePair<string, ModelState>>)_innerDictionary).IsReadOnly; }
get { return ((ICollection<KeyValuePair<string, ModelStateEntry>>)_innerDictionary).IsReadOnly; }
}
/// <inheritdoc />
@ -132,7 +132,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public ICollection<ModelState> Values
public ICollection<ModelStateEntry> Values
{
get { return _innerDictionary.Values; }
}
@ -159,7 +159,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public ModelState this[string key]
public ModelStateEntry this[string key]
{
get
{
@ -168,7 +168,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(key));
}
ModelState value;
ModelStateEntry value;
_innerDictionary.TryGetValue(key, out value);
return value;
}
@ -188,7 +188,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
// For unit testing
internal IDictionary<string, ModelState> InnerDictionary
internal IDictionary<string, ModelStateEntry> InnerDictionary
{
get { return _innerDictionary; }
}
@ -197,10 +197,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
private bool HasRecordedMaxModelError { get; set; }
/// <summary>
/// Adds the specified <paramref name="exception"/> to the <see cref="ModelState.Errors"/> instance
/// Adds the specified <paramref name="exception"/> to the <see cref="ModelStateEntry.Errors"/> instance
/// that is associated with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to add errors to.</param>
/// <param name="exception">The <see cref="Exception"/> to add.</param>
public void AddModelError(string key, Exception exception)
{
@ -218,11 +218,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Attempts to add the specified <paramref name="exception"/> to the <see cref="ModelState.Errors"/>
/// Attempts to add the specified <paramref name="exception"/> to the <see cref="ModelStateEntry.Errors"/>
/// instance that is associated with the specified <paramref name="key"/>. If the maximum number of allowed
/// errors has already been recorded, records a <see cref="TooManyModelErrorsException"/> exception instead.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to add errors to.</param>
/// <param name="exception">The <see cref="Exception"/> to add.</param>
/// <returns>
/// <c>True</c> if the given error was added, <c>false</c> if the error was ignored.
@ -249,18 +249,18 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
if (exception is FormatException || exception is OverflowException)
{
// Convert FormatExceptions and OverflowExceptions to Invalid value messages.
ModelState modelState;
TryGetValue(key, out modelState);
ModelStateEntry entry;
TryGetValue(key, out entry);
string errorMessage;
if (modelState == null)
if (entry == null)
{
errorMessage = Resources.FormatModelError_InvalidValue_GenericMessage(key);
}
else
{
errorMessage = Resources.FormatModelError_InvalidValue_MessageWithModelValue(
modelState.AttemptedValue,
entry.AttemptedValue,
key);
}
@ -273,10 +273,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Adds the specified <paramref name="errorMessage"/> to the <see cref="ModelState.Errors"/> instance
/// Adds the specified <paramref name="errorMessage"/> to the <see cref="ModelStateEntry.Errors"/> instance
/// that is associated with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to add errors to.</param>
/// <param name="errorMessage">The error message to add.</param>
public void AddModelError(string key, string errorMessage)
{
@ -294,11 +294,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Attempts to add the specified <paramref name="errorMessage"/> to the <see cref="ModelState.Errors"/>
/// Attempts to add the specified <paramref name="errorMessage"/> to the <see cref="ModelStateEntry.Errors"/>
/// instance that is associated with the specified <paramref name="key"/>. If the maximum number of allowed
/// errors has already been recorded, records a <see cref="TooManyModelErrorsException"/> exception instead.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to add errors to.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to add errors to.</param>
/// <param name="errorMessage">The error message to add.</param>
/// <returns>
/// <c>True</c> if the given error was added, <c>false</c> if the error was ignored.
@ -363,7 +363,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(key));
}
ModelState validationState;
ModelStateEntry validationState;
if (TryGetValue(key, out validationState))
{
return validationState.ValidationState;
@ -373,10 +373,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Marks the <see cref="ModelState.ValidationState"/> for the entry with the specified <paramref name="key"/>
/// as <see cref="ModelValidationState.Valid"/>.
/// Marks the <see cref="ModelStateEntry.ValidationState"/> for the entry with the specified
/// <paramref name="key"/> as <see cref="ModelValidationState.Valid"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to mark as valid.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to mark as valid.</param>
public void MarkFieldValid(string key)
{
if (key == null)
@ -394,10 +394,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Marks the <see cref="ModelState.ValidationState"/> for the entry with the specified <paramref name="key"/>
/// Marks the <see cref="ModelStateEntry.ValidationState"/> for the entry with the specified <paramref name="key"/>
/// as <see cref="ModelValidationState.Skipped"/>.
/// </summary>
/// <param name="key">The key of the <see cref="ModelState"/> to mark as skipped.</param>
/// <param name="key">The key of the <see cref="ModelStateEntry"/> to mark as skipped.</param>
public void MarkFieldSkipped(string key)
{
if (key == null)
@ -433,11 +433,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Sets the of <see cref="ModelState.RawValue"/> and <see cref="ModelState.AttemptedValue"/> for
/// the <see cref="ModelState"/> with the specified <paramref name="key"/>.
/// Sets the of <see cref="ModelStateEntry.RawValue"/> and <see cref="ModelStateEntry.AttemptedValue"/> for
/// the <see cref="ModelStateEntry"/> with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key for the <see cref="ModelState"/> entry.</param>
/// <param name="rawvalue">The raw value for the <see cref="ModelState"/> entry.</param>
/// <param name="key">The key for the <see cref="ModelStateEntry"/> entry.</param>
/// <param name="rawvalue">The raw value for the <see cref="ModelStateEntry"/> entry.</param>
/// <param name="attemptedValue">
/// The values of <param name="rawValue"/> in a comma-separated <see cref="string"/>.
/// </param>
@ -454,11 +454,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Sets the value for the <see cref="ModelState"/> with the specified <paramref name="key"/>.
/// Sets the value for the <see cref="ModelStateEntry"/> with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key for the <see cref="ModelState"/> entry</param>
/// <param name="key">The key for the <see cref="ModelStateEntry"/> entry</param>
/// <param name="valueProviderResult">
/// A <see cref="ValueProviderResult"/> with data for the <see cref="ModelState"/> entry.
/// A <see cref="ValueProviderResult"/> with data for the <see cref="ModelStateEntry"/> entry.
/// </param>
public void SetModelValue(string key, ValueProviderResult valueProviderResult)
{
@ -501,21 +501,21 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
}
private ModelState GetModelStateForKey(string key)
private ModelStateEntry GetModelStateForKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ModelState modelState;
if (!TryGetValue(key, out modelState))
ModelStateEntry entry;
if (!TryGetValue(key, out entry))
{
modelState = new ModelState();
this[key] = modelState;
entry = new ModelStateEntry();
this[key] = entry;
}
return modelState;
return entry;
}
private static ModelValidationState GetValidity(PrefixEnumerable entries, ModelValidationState defaultState)
@ -562,13 +562,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public void Add(KeyValuePair<string, ModelState> item)
public void Add(KeyValuePair<string, ModelStateEntry> item)
{
Add(item.Key, item.Value);
}
/// <inheritdoc />
public void Add(string key, ModelState value)
public void Add(string key, ModelStateEntry value)
{
if (key == null)
{
@ -590,9 +590,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public bool Contains(KeyValuePair<string, ModelState> item)
public bool Contains(KeyValuePair<string, ModelStateEntry> item)
{
return ((ICollection<KeyValuePair<string, ModelState>>)_innerDictionary).Contains(item);
return ((ICollection<KeyValuePair<string, ModelStateEntry>>)_innerDictionary).Contains(item);
}
/// <inheritdoc />
@ -607,20 +607,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex)
public void CopyTo(KeyValuePair<string, ModelStateEntry>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
((ICollection<KeyValuePair<string, ModelState>>)_innerDictionary).CopyTo(array, arrayIndex);
((ICollection<KeyValuePair<string, ModelStateEntry>>)_innerDictionary).CopyTo(array, arrayIndex);
}
/// <inheritdoc />
public bool Remove(KeyValuePair<string, ModelState> item)
public bool Remove(KeyValuePair<string, ModelStateEntry> item)
{
return ((ICollection<KeyValuePair<string, ModelState>>)_innerDictionary).Remove(item);
return ((ICollection<KeyValuePair<string, ModelStateEntry>>)_innerDictionary).Remove(item);
}
/// <inheritdoc />
@ -635,7 +635,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public bool TryGetValue(string key, out ModelState value)
public bool TryGetValue(string key, out ModelStateEntry value)
{
if (key == null)
{
@ -646,7 +646,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <inheritdoc />
public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator()
public IEnumerator<KeyValuePair<string, ModelStateEntry>> GetEnumerator()
{
return _innerDictionary.GetEnumerator();
}
@ -732,7 +732,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return new PrefixEnumerable(this, prefix);
}
public struct PrefixEnumerable : IEnumerable<KeyValuePair<string, ModelState>>
public struct PrefixEnumerable : IEnumerable<KeyValuePair<string, ModelStateEntry>>
{
private readonly ModelStateDictionary _dictionary;
private readonly string _prefix;
@ -758,7 +758,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return _dictionary == null ? new PrefixEnumerator() : new PrefixEnumerator(_dictionary, _prefix);
}
IEnumerator<KeyValuePair<string, ModelState>> IEnumerable<KeyValuePair<string, ModelState>>.GetEnumerator()
IEnumerator<KeyValuePair<string, ModelStateEntry>>
IEnumerable<KeyValuePair<string, ModelStateEntry>>.GetEnumerator()
{
return GetEnumerator();
}
@ -769,13 +770,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
}
public struct PrefixEnumerator : IEnumerator<KeyValuePair<string, ModelState>>
public struct PrefixEnumerator : IEnumerator<KeyValuePair<string, ModelStateEntry>>
{
private readonly ModelStateDictionary _dictionary;
private readonly string _prefix;
private bool _exactMatchUsed;
private Dictionary<string, ModelState>.Enumerator _enumerator;
private Dictionary<string, ModelStateEntry>.Enumerator _enumerator;
public PrefixEnumerator(ModelStateDictionary dictionary, string prefix)
{
@ -793,11 +794,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
_prefix = prefix;
_exactMatchUsed = false;
_enumerator = default(Dictionary<string, ModelState>.Enumerator);
Current = default(KeyValuePair<string, ModelState>);
_enumerator = default(Dictionary<string, ModelStateEntry>.Enumerator);
Current = default(KeyValuePair<string, ModelStateEntry>);
}
public KeyValuePair<string, ModelState> Current { get; private set; }
public KeyValuePair<string, ModelStateEntry> Current { get; private set; }
object IEnumerator.Current
{
@ -826,10 +827,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
_exactMatchUsed = true;
_enumerator = _dictionary._innerDictionary.GetEnumerator();
ModelState entry;
ModelStateEntry entry;
if (_dictionary.TryGetValue(_prefix, out entry))
{
Current = new KeyValuePair<string, ModelState>(_prefix, entry);
Current = new KeyValuePair<string, ModelStateEntry>(_prefix, entry);
return true;
}
}
@ -853,8 +854,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void Reset()
{
_exactMatchUsed = false;
_enumerator = default(Dictionary<string, ModelState>.Enumerator);
Current = default(KeyValuePair<string, ModelState>);
_enumerator = default(Dictionary<string, ModelStateEntry>.Enumerator);
Current = default(KeyValuePair<string, ModelStateEntry>);
}
}
}

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Mvc
/// <summary>
/// Creates a new instance of <see cref="SerializableError"/>.
/// </summary>
/// <param name="modelState"><see cref="ModelState"/> containing the validation errors.</param>
/// <param name="modelState"><see cref="ModelStateEntry"/> containing the validation errors.</param>
public SerializableError(ModelStateDictionary modelState)
: this()
{

View File

@ -10,30 +10,30 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures.Internal
{
internal static class ValidationHelpers
{
public static string GetUserErrorMessageOrDefault(ModelError modelError, ModelState modelState)
public static string GetUserErrorMessageOrDefault(ModelError modelError, ModelStateEntry entry)
{
if (!string.IsNullOrEmpty(modelError.ErrorMessage))
{
return modelError.ErrorMessage;
}
if (modelState == null)
if (entry == null)
{
return string.Empty;
}
var attemptedValue = modelState.AttemptedValue ?? "null";
var attemptedValue = entry.AttemptedValue ?? "null";
return Resources.FormatCommon_ValueNotValidForProperty(attemptedValue);
}
// Returns non-null list of model states, which caller will render in order provided.
public static IEnumerable<ModelState> GetModelStateList(
public static IEnumerable<ModelStateEntry> GetModelStateList(
ViewDataDictionary viewData,
bool excludePropertyErrors)
{
if (excludePropertyErrors)
{
ModelState ms;
ModelStateEntry ms;
viewData.ModelState.TryGetValue(viewData.TemplateInfo.HtmlFieldPrefix, out ms);
if (ms != null)
@ -41,7 +41,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures.Internal
return new[] { ms };
}
return Enumerable.Empty<ModelState>();
return Enumerable.Empty<ModelStateEntry>();
}
else
{
@ -58,7 +58,8 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures.Internal
// ModelState doesn't refer to ModelMetadata, but we can correlate via the property name.
private class ErrorsOrderer
{
private Dictionary<string, int> _ordering = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, int> _ordering =
new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
public ErrorsOrderer(ModelMetadata metadata)
{

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public static class ModelStateDictionaryExtensions
{
/// <summary>
/// Adds the specified <paramref name="errorMessage"/> to the <see cref="ModelState.Errors"/> instance
/// Adds the specified <paramref name="errorMessage"/> to the <see cref="ModelStateEntry.Errors"/> instance
/// that is associated with the specified <paramref name="expression"/>.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
/// <summary>
/// Adds the specified <paramref name="exception"/> to the <see cref="ModelState.Errors"/> instance
/// Adds the specified <paramref name="exception"/> to the <see cref="ModelStateEntry.Errors"/> instance
/// that is associated with the specified <paramref name="expression"/>.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>

View File

@ -546,10 +546,10 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
}
// If there are any errors for a named field, we add the css attribute.
ModelState modelState;
if (viewContext.ViewData.ModelState.TryGetValue(fullName, out modelState))
ModelStateEntry entry;
if (viewContext.ViewData.ModelState.TryGetValue(fullName, out entry))
{
if (modelState.Errors.Count > 0)
if (entry.Errors.Count > 0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
@ -599,13 +599,13 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
nameof(expression));
}
ModelState modelState;
viewContext.ViewData.ModelState.TryGetValue(fullName, out modelState);
ModelStateEntry entry;
viewContext.ViewData.ModelState.TryGetValue(fullName, out entry);
var value = string.Empty;
if (modelState != null && modelState.AttemptedValue != null)
if (entry != null && entry.AttemptedValue != null)
{
value = modelState.AttemptedValue;
value = entry.AttemptedValue;
}
else if (modelExplorer.Model != null)
{
@ -629,7 +629,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
tagBuilder.MergeAttributes(GetValidationAttributes(viewContext, modelExplorer, expression));
// If there are any errors for a named field, we add this CSS attribute.
if (modelState != null && modelState.Errors.Count > 0)
if (entry != null && entry.Errors.Count > 0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
@ -703,9 +703,9 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
return null;
}
ModelState modelState;
var tryGetModelStateResult = viewContext.ViewData.ModelState.TryGetValue(fullName, out modelState);
var modelErrors = tryGetModelStateResult ? modelState.Errors : null;
ModelStateEntry entry;
var tryGetModelStateResult = viewContext.ViewData.ModelState.TryGetValue(fullName, out entry);
var modelErrors = tryGetModelStateResult ? entry.Errors : null;
ModelError modelError = null;
if (modelErrors != null && modelErrors.Count != 0)
@ -741,7 +741,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
else if (modelError != null)
{
tagBuilder.InnerHtml.SetContent(
ValidationHelpers.GetUserErrorMessageOrDefault(modelError, modelState));
ValidationHelpers.GetUserErrorMessageOrDefault(modelError, entry));
}
if (formContext != null)
@ -804,7 +804,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
for (var i = 0; i < modelState.Errors.Count; i++)
{
var modelError = modelState.Errors[i];
var errorText = ValidationHelpers.GetUserErrorMessageOrDefault(modelError, modelState: null);
var errorText = ValidationHelpers.GetUserErrorMessageOrDefault(modelError, entry: null);
if (!string.IsNullOrEmpty(errorText))
{
@ -1056,10 +1056,10 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
internal static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
{
ModelState modelState;
if (viewContext.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.RawValue != null)
ModelStateEntry entry;
if (viewContext.ViewData.ModelState.TryGetValue(key, out entry) && entry.RawValue != null)
{
return ModelBindingHelper.ConvertTo(modelState.RawValue, destinationType, culture: null);
return ModelBindingHelper.ConvertTo(entry.RawValue, destinationType, culture: null);
}
return null;
@ -1228,8 +1228,8 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
}
// If there are any errors for a named field, we add the CSS attribute.
ModelState modelState;
if (viewContext.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0)
ModelStateEntry entry;
if (viewContext.ViewData.ModelState.TryGetValue(fullName, out entry) && entry.Errors.Count > 0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}

View File

@ -95,7 +95,7 @@ namespace System.Web.Http
Message = ShimResources.HttpError_BadRequest;
var modelStateError = new HttpError();
foreach (KeyValuePair<string, ModelState> keyModelStatePair in modelState)
foreach (KeyValuePair<string, ModelStateEntry> keyModelStatePair in modelState)
{
var key = keyModelStatePair.Key;
var errors = keyModelStatePair.Value.Errors;

View File

@ -15,14 +15,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MarkFieldSkipped_MarksFieldAsSkipped_IfStateIsNotInValid(ModelValidationState validationState)
{
// Arrange
var modelState = new ModelState
var entry = new ModelStateEntry
{
ValidationState = validationState
};
var source = new ModelStateDictionary
{
{ "key", modelState }
{ "key", entry }
};
// Act
@ -36,14 +36,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MarkFieldSkipped_MarksFieldAsSkipped_IfKeyIsNotPresent()
{
// Arrange
var modelState = new ModelState
var entry = new ModelStateEntry
{
ValidationState = ModelValidationState.Valid
};
var source = new ModelStateDictionary
{
};
var source = new ModelStateDictionary();
// Act
source.MarkFieldSkipped("key");
@ -58,14 +56,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MarkFieldSkipped_Throws_IfStateIsInvalid()
{
// Arrange
var modelState = new ModelState
var entry = new ModelStateEntry
{
ValidationState = ModelValidationState.Invalid
};
var source = new ModelStateDictionary
{
{ "key", modelState }
{ "key", entry }
};
// Act
@ -83,14 +81,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MarkFieldValid_MarksFieldAsValid_IfStateIsNotInvalid(ModelValidationState validationState)
{
// Arrange
var modelState = new ModelState
var entry = new ModelStateEntry
{
ValidationState = validationState
};
var source = new ModelStateDictionary
{
{ "key", modelState }
{ "key", entry }
};
// Act
@ -119,14 +117,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MarkFieldValid_Throws_IfStateIsInvalid()
{
// Arrange
var modelState = new ModelState
var entry = new ModelStateEntry
{
ValidationState = ModelValidationState.Invalid
};
var source = new ModelStateDictionary
{
{ "key", modelState }
{ "key", entry }
};
// Act
@ -142,10 +140,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void CopyConstructor_CopiesModelStateData()
{
// Arrange
var modelState = new ModelState();
var entry = new ModelStateEntry();
var source = new ModelStateDictionary
{
{ "key", modelState }
{ "key", entry }
};
// Act
@ -154,8 +152,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Assert
Assert.Equal(0, target.ErrorCount);
Assert.Equal(1, target.Count);
Assert.Same(modelState, target["key"]);
Assert.IsType<Dictionary<string, ModelState>>(target.InnerDictionary);
Assert.Same(entry, target["key"]);
Assert.IsType<Dictionary<string, ModelStateEntry>>(target.InnerDictionary);
}
[Fact]
@ -202,7 +200,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var oldDictionary = new ModelStateDictionary()
{
{ "foo", new ModelState() { RawValue = "bar" } }
{ "foo", new ModelStateEntry() { RawValue = "bar" } }
};
// Act
@ -217,10 +215,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetFieldValidationState_ReturnsUnvalidatedIfDictionaryDoesNotContainKey()
{
// Arrange
var msd = new ModelStateDictionary();
var dictionary = new ModelStateDictionary();
// Act
var validationState = msd.GetFieldValidationState("foo");
var validationState = dictionary.GetFieldValidationState("foo");
// Assert
Assert.Equal(ModelValidationState.Unvalidated, validationState);
@ -230,11 +228,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetValidationState_ReturnsValidationStateForKey_IgnoresChildren()
{
// Arrange
var msd = new ModelStateDictionary();
msd.AddModelError("foo.bar", "error text");
var dictionary = new ModelStateDictionary();
dictionary.AddModelError("foo.bar", "error text");
// Act
var validationState = msd.GetValidationState("foo");
var validationState = dictionary.GetValidationState("foo");
// Assert
Assert.Equal(ModelValidationState.Unvalidated, validationState);
@ -248,11 +246,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetFieldValidationState_ReturnsInvalidIfKeyChildContainsErrors(string key)
{
// Arrange
var msd = new ModelStateDictionary();
msd.AddModelError(key, "error text");
var dictionary = new ModelStateDictionary();
dictionary.AddModelError(key, "error text");
// Act
var validationState = msd.GetFieldValidationState("foo");
var validationState = dictionary.GetFieldValidationState("foo");
// Assert
Assert.Equal(ModelValidationState.Invalid, validationState);
@ -266,17 +264,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetFieldValidationState_ReturnsValidIfModelStateDoesNotContainErrors(string key)
{
// Arrange
var validState = new ModelState
var validState = new ModelStateEntry
{
ValidationState = ModelValidationState.Valid
};
var msd = new ModelStateDictionary
var dictionary = new ModelStateDictionary
{
{ key, validState }
};
// Act
var validationState = msd.GetFieldValidationState("foo");
var validationState = dictionary.GetFieldValidationState("foo");
// Assert
Assert.Equal(ModelValidationState.Valid, validationState);
@ -288,11 +286,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetFieldValidationState_IndexedPrefix_ReturnsInvalidIfKeyChildContainsErrors(string key)
{
// Arrange
var msd = new ModelStateDictionary();
msd.AddModelError(key, "error text");
var dictionary = new ModelStateDictionary();
dictionary.AddModelError(key, "error text");
// Act
var validationState = msd.GetFieldValidationState("[0].foo");
var validationState = dictionary.GetFieldValidationState("[0].foo");
// Assert
Assert.Equal(ModelValidationState.Invalid, validationState);
@ -304,17 +302,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetFieldValidationState_IndexedPrefix_ReturnsValidIfModelStateDoesNotContainErrors(string key)
{
// Arrange
var validState = new ModelState
var validState = new ModelStateEntry
{
ValidationState = ModelValidationState.Valid
};
var msd = new ModelStateDictionary
var dictionary = new ModelStateDictionary
{
{ key, validState }
};
// Act
var validationState = msd.GetFieldValidationState("[0].foo");
var validationState = dictionary.GetFieldValidationState("[0].foo");
// Assert
Assert.Equal(ModelValidationState.Valid, validationState);
@ -324,11 +322,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void IsValidPropertyReturnsFalseIfErrors()
{
// Arrange
var errorState = new ModelState
var errorState = new ModelStateEntry
{
ValidationState = ModelValidationState.Invalid
};
var validState = new ModelState
var validState = new ModelStateEntry
{
ValidationState = ModelValidationState.Valid
};
@ -354,15 +352,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var dictionary = new ModelStateDictionary()
{
{ "foo", new ModelState
{
ValidationState = ModelValidationState.Valid,
}
{ "foo", new ModelStateEntry
{
ValidationState = ModelValidationState.Valid,
}
},
{ "baz", new ModelState
{
ValidationState = ModelValidationState.Skipped,
}
{ "baz", new ModelStateEntry
{
ValidationState = ModelValidationState.Skipped,
}
}
};
@ -379,11 +377,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void IsValidPropertyReturnsFalse_IfSomeFieldsAreNotValidated()
{
// Arrange
var errorState = new ModelState
var errorState = new ModelStateEntry
{
ValidationState = ModelValidationState.Invalid
};
var validState = new ModelState
var validState = new ModelStateEntry
{
ValidationState = ModelValidationState.Valid
};
@ -392,7 +390,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
{ "foo", validState },
{ "baz", errorState },
{ "qux", new ModelState() }
{ "qux", new ModelStateEntry() }
};
// Act
@ -408,29 +406,29 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void MergeCopiesDictionaryEntries()
{
// Arrange
var fooDict = new ModelStateDictionary() { { "foo", new ModelState() } };
var barDict = new ModelStateDictionary() { { "bar", new ModelState() } };
var dictionary1 = new ModelStateDictionary { { "foo", new ModelStateEntry() } };
var dictionary2 = new ModelStateDictionary { { "bar", new ModelStateEntry() } };
// Act
fooDict.Merge(barDict);
dictionary1.Merge(dictionary2);
// Assert
Assert.Equal(2, fooDict.Count);
Assert.Equal(barDict["bar"], fooDict["bar"]);
Assert.Equal(2, dictionary1.Count);
Assert.Equal(dictionary2["bar"], dictionary1["bar"]);
}
[Fact]
public void MergeDoesNothingIfParameterIsNull()
{
// Arrange
var fooDict = new ModelStateDictionary() { { "foo", new ModelState() } };
var dictionary = new ModelStateDictionary() { { "foo", new ModelStateEntry() } };
// Act
fooDict.Merge(null);
dictionary.Merge(null);
// Assert
Assert.Single(fooDict);
Assert.True(fooDict.ContainsKey("foo"));
Assert.Single(dictionary);
Assert.True(dictionary.ContainsKey("foo"));
}
[Fact]
@ -491,7 +489,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["user.Address"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["user.Address"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary.SetModelValue("user.Name", new string[] { "some value" }, "some value");
dictionary.AddModelError("user.Age", "Age is not a valid int");
@ -510,11 +508,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["user.Address"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["user.Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["user.Address"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["user.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary.AddModelError("user.Age", "Age is not a valid int");
dictionary["[0].product.Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["[0].product.Age[0]"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["[0].product.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["[0].product.Age[0]"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary.AddModelError("[1].product.Name", "Name is invalid");
// Act
@ -529,8 +527,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["user.Address"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["user.Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["user.Address"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["user.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
// Act
var validationState = dictionary.GetFieldValidationState("user");
@ -761,15 +759,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["Property1"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["Property1"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["Property2"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Property2"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Property2", "Property2 invalid.");
dictionary["Property3"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Property3"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Property3", "Property invalid.");
dictionary["Property4"] = new ModelState { ValidationState = ModelValidationState.Skipped };
dictionary["Property4"] = new ModelStateEntry { ValidationState = ModelValidationState.Skipped };
// Act
dictionary.ClearValidationState("Property1");
@ -793,22 +791,22 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["Product"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["Product"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["Product.Detail1"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Product.Detail1"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Product.Detail1", "Product Detail1 invalid.");
dictionary["Product.Detail2[0]"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Product.Detail2[0]"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Product.Detail2[0]", "Product Detail2[0] invalid.");
dictionary["Product.Detail2[1]"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Product.Detail2[1]"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Product.Detail2[1]", "Product Detail2[1] invalid.");
dictionary["Product.Detail2[2]"] = new ModelState { ValidationState = ModelValidationState.Skipped };
dictionary["Product.Detail2[2]"] = new ModelStateEntry { ValidationState = ModelValidationState.Skipped };
dictionary["Product.Detail3"] = new ModelState { ValidationState = ModelValidationState.Skipped };
dictionary["Product.Detail3"] = new ModelStateEntry { ValidationState = ModelValidationState.Skipped };
dictionary["ProductName"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["ProductName"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("ProductName", "ProductName invalid.");
// Act
@ -837,15 +835,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["Product"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["Product"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["Product.Detail1"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Product.Detail1"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Product.Detail1", "Product Detail1 invalid.");
dictionary["Product.Detail1.Name"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Product.Detail1.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Product.Detail1.Name", "Product Detail1 Name invalid.");
dictionary["Product.Detail1Name"] = new ModelState { ValidationState = ModelValidationState.Skipped };
dictionary["Product.Detail1Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Skipped };
// Act
dictionary.ClearValidationState("Product.Detail1");
@ -867,15 +865,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var dictionary = new ModelStateDictionary();
dictionary["Property1"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["Property1"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["Property2"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Property2"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Property2", "Property2 invalid.");
dictionary["Property3"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Property3"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Property3", "Property invalid.");
dictionary["Property4"] = new ModelState { ValidationState = ModelValidationState.Skipped };
dictionary["Property4"] = new ModelStateEntry { ValidationState = ModelValidationState.Skipped };
// Act
dictionary.ClearValidationState(modelKey);

View File

@ -715,12 +715,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var dictionary = new ModelStateDictionary();
dictionary["Name"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Name", "MyProperty invalid.");
dictionary["Id"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["Id"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("Id", "Id invalid.");
dictionary.AddModelError("Id", "Id is required.");
dictionary["Category"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["Category"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
// Act
ModelBindingHelper.ClearValidationStateForModel(
@ -746,16 +746,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var dictionary = new ModelStateDictionary();
dictionary["[0].Name"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["[0].Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("[0].Name", "Name invalid.");
dictionary["[0].Id"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["[0].Id"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("[0].Id", "Id invalid.");
dictionary.AddModelError("[0].Id", "Id required.");
dictionary["[0].Category"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["[0].Category"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["[1].Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["[1].Id"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["[1].Category"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["[1].Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["[1].Id"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["[1].Category"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("[1].Category", "Category invalid.");
// Act
@ -793,20 +793,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var metadataProvider = new TestModelMetadataProvider();
var dictionary = new ModelStateDictionary();
dictionary["product.Name"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["product.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("product.Name", "Name invalid.");
dictionary["product.Id"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["product.Id"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("product.Id", "Id invalid.");
dictionary.AddModelError("product.Id", "Id required.");
dictionary["product.Category"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["product.Category.Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["product.Order[0].Name"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["product.Category"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["product.Category.Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["product.Order[0].Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("product.Order[0].Name", "Order name invalid.");
dictionary["product.Order[0].Address.Street"] =
new ModelState { ValidationState = ModelValidationState.Invalid };
new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("product.Order[0].Address.Street", "Street invalid.");
dictionary["product.Order[1].Name"] = new ModelState { ValidationState = ModelValidationState.Valid };
dictionary["product.Order[0]"] = new ModelState { ValidationState = ModelValidationState.Invalid };
dictionary["product.Order[1].Name"] = new ModelStateEntry { ValidationState = ModelValidationState.Valid };
dictionary["product.Order[0]"] = new ModelStateEntry { ValidationState = ModelValidationState.Invalid };
dictionary.AddModelError("product.Order[0]", "Order invalid.");
// Act

View File

@ -809,9 +809,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Single(modelStateDictionary);
// Check Age error.
ModelState modelState;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out modelState));
var modelError = Assert.Single(modelState.Errors);
ModelStateEntry entry;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out entry));
var modelError = Assert.Single(entry.Errors);
Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage);
Assert.Equal("A value for the 'Age' property was not provided.", modelError.ErrorMessage);
@ -859,9 +859,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Single(modelStateDictionary);
// Check Age error.
ModelState modelState;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out modelState));
var modelError = Assert.Single(modelState.Errors);
ModelStateEntry entry;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out entry));
var modelError = Assert.Single(entry.Errors);
Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage);
Assert.Equal("A value for the 'Age' property was not provided.", modelError.ErrorMessage);
@ -914,11 +914,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal(1, modelStateDictionary.Count);
// Check Age error.
ModelState modelState;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out modelState));
Assert.Equal(ModelValidationState.Invalid, modelState.ValidationState);
ModelStateEntry entry;
Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out entry));
Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
var modelError = Assert.Single(modelState.Errors);
var modelError = Assert.Single(entry.Errors);
Assert.Equal(string.Empty, modelError.ErrorMessage);
Assert.IsType<NullReferenceException>(modelError.Exception);
}

View File

@ -651,9 +651,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
var validator = CreateValidator(new SimpleTypesExcludeFilter());
modelState.Add("items[0]", new ModelState());
modelState.Add("items[1]", new ModelState());
modelState.Add("items[2]", new ModelState());
modelState.Add("items[0]", new ModelStateEntry());
modelState.Add("items[1]", new ModelStateEntry());
modelState.Add("items[2]", new ModelStateEntry());
validationState.Add(model, new ValidationStateEntry()
{
Key = "items",
@ -698,10 +698,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{ "BarKey", "BarValue" }
};
modelState.Add("items[0].Key", new ModelState());
modelState.Add("items[0].Value", new ModelState());
modelState.Add("items[1].Key", new ModelState());
modelState.Add("items[1].Value", new ModelState());
modelState.Add("items[0].Key", new ModelStateEntry());
modelState.Add("items[0].Value", new ModelStateEntry());
modelState.Add("items[1].Key", new ModelStateEntry());
modelState.Add("items[1].Value", new ModelStateEntry());
validationState.Add(model, new ValidationStateEntry() { Key = "items" });
// Act

View File

@ -72,10 +72,10 @@ namespace Microsoft.AspNet.Mvc
var modelState = new ModelStateDictionary();
modelState.Add(
"key1",
new ModelState());
new ModelStateEntry());
modelState.Add(
"key2",
new ModelState());
new ModelStateEntry());
// Act
var serializableError = new SerializableError(modelState);

View File

@ -155,7 +155,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary.Add("Text", new ModelState());
dictionary.Add("Text", new ModelStateEntry());
// Act
dictionary.Remove<TestModel>(model => model.Text);
@ -169,7 +169,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary.Add("Child.Text", new ModelState());
dictionary.Add("Child.Text", new ModelStateEntry());
// Act
dictionary.Remove<TestModel>(model => model.Child.Text);
@ -183,7 +183,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var dictionary = new ModelStateDictionary();
dictionary.Add("Child.Value", new ModelState());
dictionary.Add("Child.Value", new ModelStateEntry());
// Act
dictionary.Remove<TestModel>(model => model.Child.Value);
@ -198,7 +198,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Arrange
var variable = "Test";
var dictionary = new ModelStateDictionary();
dictionary.Add("variable", new ModelState());
dictionary.Add("variable", new ModelStateEntry());
// Act
dictionary.Remove<TestModel>(model => variable);
@ -211,12 +211,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void RemoveAll_ForSingleExpression_RemovesModelStateKeys()
{
// Arrange
var state = new ModelState();
var state = new ModelStateEntry();
var dictionary = new ModelStateDictionary();
dictionary.Add("Key", state);
dictionary.Add("Text", new ModelState());
dictionary.Add("Text.Length", new ModelState());
dictionary.Add("Text", new ModelStateEntry());
dictionary.Add("Text.Length", new ModelStateEntry());
// Act
dictionary.RemoveAll<TestModel>(model => model.Text);
@ -232,12 +232,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void RemoveAll_ForRelationExpression_RemovesModelStateKeys()
{
// Arrange
var state = new ModelState();
var state = new ModelStateEntry();
var dictionary = new ModelStateDictionary();
dictionary.Add("Key", state);
dictionary.Add("Child", new ModelState());
dictionary.Add("Child.Text", new ModelState());
dictionary.Add("Child", new ModelStateEntry());
dictionary.Add("Child.Text", new ModelStateEntry());
// Act
dictionary.RemoveAll<TestModel>(model => model.Child);
@ -253,11 +253,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void RemoveAll_ForImplicitlyCastedToObjectExpression_RemovesModelStateKeys()
{
// Arrange
var state = new ModelState();
var state = new ModelStateEntry();
var dictionary = new ModelStateDictionary();
dictionary.Add("Child", state);
dictionary.Add("Child.Value", new ModelState());
dictionary.Add("Child.Value", new ModelStateEntry());
// Act
dictionary.RemoveAll<TestModel>(model => model.Child.Value);
@ -274,13 +274,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
// Arrange
var variable = "Test";
var state = new ModelState();
var state = new ModelStateEntry();
var dictionary = new ModelStateDictionary();
dictionary.Add("Key", state);
dictionary.Add("variable", new ModelState());
dictionary.Add("variable.Text", new ModelState());
dictionary.Add("variable.Value", new ModelState());
dictionary.Add("variable", new ModelStateEntry());
dictionary.Add("variable.Text", new ModelStateEntry());
dictionary.Add("variable.Value", new ModelStateEntry());
// Act
dictionary.RemoveAll<TestModel>(model => variable);
@ -296,14 +296,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void RemoveAll_ForModelExpression_RemovesModelPropertyKeys()
{
// Arrange
var state = new ModelState();
var state = new ModelStateEntry();
var dictionary = new ModelStateDictionary();
dictionary.Add("Key", state);
dictionary.Add("Text", new ModelState());
dictionary.Add("Child", new ModelState());
dictionary.Add("Child.Text", new ModelState());
dictionary.Add("Child.NoValue", new ModelState());
dictionary.Add("Text", new ModelStateEntry());
dictionary.Add("Child", new ModelStateEntry());
dictionary.Add("Child.Text", new ModelStateEntry());
dictionary.Add("Child.NoValue", new ModelStateEntry());
// Act
dictionary.RemoveAll<TestModel>(model => model);

View File

@ -260,7 +260,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
viewData["FieldPrefix.Name"] = "View data dictionary value";
viewData.TemplateInfo.HtmlFieldPrefix = "FieldPrefix";
var modelState = new ModelState();
var modelState = new ModelStateEntry();
modelState.RawValue = new string[] { "Attempted name value" };
modelState.AttemptedValue = "Attempted name value";
viewData.ModelState["FieldPrefix.Name"] = modelState;
@ -285,7 +285,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
viewData["Name"] = "View data dictionary value";
viewData.TemplateInfo.HtmlFieldPrefix = "FieldPrefix";
var modelState = new ModelState();
var modelState = new ModelStateEntry();
modelState.RawValue = new string[] { "Attempted name value" };
modelState.AttemptedValue = "Attempted name value";
viewData.ModelState["FieldPrefix.Name"] = modelState;

View File

@ -362,9 +362,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
idAttributeDotReplacement: "$");
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
helper.ViewData.ModelState.Clear();
helper.ViewData.ModelState.Add("Property1", GetModelState("modelstate-without-prefix"));
helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelState("modelstate-with-prefix"));
helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelState("modelstate-with-iddotreplacement"));
helper.ViewData.ModelState.Add("Property1", GetModelStateEntry("modelstate-without-prefix"));
helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelStateEntry("modelstate-with-prefix"));
helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelStateEntry("modelstate-with-iddotreplacement"));
// Act
var result = helper.Hidden("Property1", "explicit-value", htmlAttributes: null);
@ -661,9 +661,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
helper.ViewData.Model.Property1 = "propValue";
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
helper.ViewData.ModelState.Clear();
helper.ViewData.ModelState.Add("Property1", GetModelState("modelstate-without-prefix"));
helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelState("modelstate-with-prefix"));
helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelState("modelstate-with-iddotreplacement"));
helper.ViewData.ModelState.Add("Property1", GetModelStateEntry("modelstate-without-prefix"));
helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelStateEntry("modelstate-with-prefix"));
helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelStateEntry("modelstate-with-iddotreplacement"));
// Act
var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null);
@ -803,9 +803,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
// Arrange
var viewData = GetViewDataWithNullModelAndNonNullViewData();
viewData.ModelState.Add("pre.Property3[key]", GetModelState("Prop3Val"));
viewData.ModelState.Add("pre.Property4.Property5", GetModelState("Prop5Val"));
viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelState("Prop6Val"));
viewData.ModelState.Add("pre.Property3[key]", GetModelStateEntry("Prop3Val"));
viewData.ModelState.Add("pre.Property4.Property5", GetModelStateEntry("Prop5Val"));
viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelStateEntry("Prop6Val"));
var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData);
viewData.TemplateInfo.HtmlFieldPrefix = "pre";
@ -858,7 +858,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
var viewData = GetViewDataWithNonNullModel();
viewData["Property1"] = "view-data-val";
viewData.ModelState.Add("Property1", GetModelState("ModelStateValue"));
viewData.ModelState.Add("Property1", GetModelStateEntry("ModelStateValue"));
return viewData;
}
@ -871,9 +871,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
return viewData;
}
private static ModelState GetModelState(string value)
private static ModelStateEntry GetModelStateEntry(string value)
{
return new ModelState
return new ModelStateEntry
{
RawValue = new string[] { value },
AttemptedValue = value,

View File

@ -325,9 +325,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
// Arrange
var viewData = GetViewDataWithModelStateAndModelAndViewDataValues();
viewData.ModelState.Add("pre.Property3[key]", GetModelState("Property3Val"));
viewData.ModelState.Add("pre.Property4.Property5", GetModelState("Property5Val"));
viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelState("Property6Val"));
viewData.ModelState.Add("pre.Property3[key]", GetModelStateEntry("Property3Val"));
viewData.ModelState.Add("pre.Property4.Property5", GetModelStateEntry("Property5Val"));
viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelStateEntry("Property6Val"));
viewData["pre.Property3[key]"] = "vdd-value1";
viewData["pre.Property4.Property5"] = "vdd-value2";
viewData["pre.Property4.Property6[0]"] = "vdd-value3";
@ -358,7 +358,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
var viewData = GetViewDataWithNullModelAndNonEmptyViewData();
viewData.Model = new PasswordModel();
viewData.ModelState.Add("Property1", GetModelState("ModelStateValue"));
viewData.ModelState.Add("Property1", GetModelStateEntry("ModelStateValue"));
return viewData;
}
@ -371,9 +371,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
return viewData;
}
private static ModelState GetModelState(string value)
private static ModelStateEntry GetModelStateEntry(string value)
{
return new ModelState
return new ModelStateEntry
{
RawValue = new string[] { value },
AttemptedValue = value,

View File

@ -414,12 +414,12 @@ namespace Microsoft.AspNet.Mvc.Rendering
var modelState = new ModelStateDictionary
{
["Property1"] = new ModelState
["Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntry.ToString() },
AttemptedValue = SelectSources.ModelStateEntry.ToString()
},
["Prefix.Property1"] = new ModelState
["Prefix.Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntryWithPrefix.ToString() },
AttemptedValue = SelectSources.ModelStateEntryWithPrefix.ToString()
@ -455,12 +455,12 @@ namespace Microsoft.AspNet.Mvc.Rendering
var modelState = new ModelStateDictionary
{
["Property1"] = new ModelState
["Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntry.ToString() },
AttemptedValue = SelectSources.ModelStateEntry.ToString()
},
["Prefix.Property1"] = new ModelState
["Prefix.Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntryWithPrefix.ToString() },
AttemptedValue = SelectSources.ModelStateEntryWithPrefix.ToString()
@ -825,12 +825,12 @@ namespace Microsoft.AspNet.Mvc.Rendering
var modelState = new ModelStateDictionary
{
["Property1"] = new ModelState
["Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntry.ToString() },
AttemptedValue = SelectSources.ModelStateEntry.ToString()
},
["Prefix.Property1"] = new ModelState
["Prefix.Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntryWithPrefix.ToString() },
AttemptedValue = SelectSources.ModelStateEntryWithPrefix.ToString()
@ -866,12 +866,12 @@ namespace Microsoft.AspNet.Mvc.Rendering
var modelState = new ModelStateDictionary
{
["Property1"] = new ModelState
["Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntry.ToString() },
AttemptedValue = SelectSources.ModelStateEntry.ToString()
},
["Prefix.Property1"] = new ModelState
["Prefix.Property1"] = new ModelStateEntry
{
RawValue = new string[] { SelectSources.ModelStateEntryWithPrefix.ToString() },
AttemptedValue = SelectSources.ModelStateEntryWithPrefix.ToString()

View File

@ -163,12 +163,12 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
viewData["StringProperty"] = "ViewDataValue";
viewData.TemplateInfo.HtmlFieldPrefix = "FieldPrefix";
var modelState = new ModelState();
var modelState = new ModelStateEntry();
modelState.AttemptedValue = "StringPropertyAttemptedValue";
modelState.RawValue = new string[] { "StringPropertyRawValue" };
viewData.ModelState["FieldPrefix.StringProperty"] = modelState;
modelState = new ModelState();
modelState = new ModelStateEntry();
modelState.AttemptedValue = "ModelAttemptedValue";
modelState.RawValue = new string[] { "ModelRawValue" };
viewData.ModelState["FieldPrefix"] = modelState;
@ -220,7 +220,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
var viewData = helper.ViewData;
viewData["StringProperty"] = "ViewDataValue <\"\">";
var modelState = new ModelState();
var modelState = new ModelStateEntry();
modelState.AttemptedValue = "ObjectPropertyAttemptedValue <\"\">";
modelState.RawValue = new string[] { "ObjectPropertyRawValue <\"\">" };
viewData.ModelState["ObjectProperty"] = modelState;

View File

@ -33,7 +33,12 @@ namespace System.Web.Http.Dispatcher
yield return new[] { new HttpError() };
yield return new[] { new HttpError("error") };
yield return new[] { new HttpError(new NotImplementedException(), true) };
yield return new[] { new HttpError(new ModelStateDictionary() { { "key", new ModelState() { Errors = { new ModelError("error") } } } }, true) };
yield return new[] { new HttpError(
new ModelStateDictionary()
{
{ "key", new ModelStateEntry { Errors = { new ModelError("error") } } }
},
true) };
}
}