Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Make ValueProviderResult a string-ish struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rynowak committed Aug 21, 2015
1 parent 02cc82a commit 6d365e9
Show file tree
Hide file tree
Showing 73 changed files with 2,106 additions and 1,777 deletions.
2 changes: 1 addition & 1 deletion NuGet.Config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AspNetVNext" value="https://www.myget.org/F/aspnetlitedev/api/v2" />
Expand Down
24 changes: 18 additions & 6 deletions src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@

namespace Microsoft.AspNet.Mvc.ModelBinding
{
/// <summary>
/// An entry in a <see cref="ModelStateDictionary"/>.
/// </summary>
public class ModelState
{
private readonly ModelErrorCollection _errors = new ModelErrorCollection();
/// <summary>
/// Gets the raw value from the request associated with this entry.
/// </summary>
public object RawValue { get; set; }

public ValueProviderResult Value { get; set; }
/// <summary>
/// Gets the set of values contained in <see cref="RawValue"/>, joined into a comma-separated string.
/// </summary>
public string AttemptedValue { get; set; }

public ModelErrorCollection Errors
{
get { return _errors; }
}
/// <summary>
/// Gets the <see cref="ModelErrorCollection"/> for this entry.
/// </summary>
public ModelErrorCollection Errors { get; } = new ModelErrorCollection();

/// <summary>
/// Gets or sets the <see cref="ModelValidationState"/> for this entry.
/// </summary>
public ModelValidationState ValidationState { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Framework.Internal;
Expand Down Expand Up @@ -213,6 +214,7 @@ public bool TryAddModelError([NotNull] string key, [NotNull] Exception exception
// Convert FormatExceptions to Invalid value messages.
ModelState modelState;
TryGetValue(key, out modelState);

string errorMessage;
if (modelState == null)
{
Expand All @@ -221,7 +223,7 @@ public bool TryAddModelError([NotNull] string key, [NotNull] Exception exception
else
{
errorMessage = Resources.FormatModelError_InvalidValue_MessageWithModelValue(
modelState.Value.AttemptedValue,
modelState.AttemptedValue,
key);
}

Expand Down Expand Up @@ -359,14 +361,50 @@ public void Merge(ModelStateDictionary dictionary)
}

/// <summary>
/// Sets the value for the <see cref="ModelState"/> with the specified <paramref name="key"/> to the
/// specified <paramref name="value"/>.
/// Sets the of <see cref="ModelState.RawValue"/> and <see cref="ModelState.AttemptedValue"/> for
/// the <see cref="ModelState"/> with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key for the <see cref="ModelState"/> entry.</param>
/// <param name="value">The value to assign.</param>
public void SetModelValue([NotNull] string key, [NotNull] ValueProviderResult value)
/// <param name="rawvalue">The raw value for the <see cref="ModelState"/> entry.</param>
/// <param name="attemptedValue">
/// The values of <param name="rawValue"/> in a comma-separated <see cref="string"/>.
/// </param>
public void SetModelValue([NotNull] string key, object rawValue, string attemptedValue)
{
GetModelStateForKey(key).Value = value;
var modelState = GetModelStateForKey(key);
modelState.RawValue = rawValue;
modelState.AttemptedValue = attemptedValue;
}

/// <summary>
/// Sets the value for the <see cref="ModelState"/> with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key for the <see cref="ModelState"/> entry</param>
/// <param name="valueProviderResult">
/// A <see cref="ValueProviderResult"/> with data for the <see cref="ModelState"/> entry.
/// </param>
public void SetModelValue([NotNull] string key, ValueProviderResult valueProviderResult)
{
// Avoid creating a new array for rawvalue if there's only one value.
object rawValue;
if (valueProviderResult == ValueProviderResult.None)
{
rawValue = null;
}
else if (valueProviderResult.Value != null)
{
rawValue = valueProviderResult.Value;
}
else if (valueProviderResult.Length == 1)
{
rawValue = valueProviderResult.Values[0];
}
else
{
rawValue = valueProviderResult.Values;
}

SetModelValue(key, rawValue, (string)valueProviderResult);
}

/// <summary>
Expand Down
Loading

0 comments on commit 6d365e9

Please sign in to comment.