Skip to content

Commit 6d365e9

Browse files
committed
Make ValueProviderResult a string-ish struct
1 parent 02cc82a commit 6d365e9

File tree

73 files changed

+2106
-1777
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2106
-1777
lines changed

NuGet.Config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
44
<add key="AspNetVNext" value="https://www.myget.org/F/aspnetlitedev/api/v2" />

src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelState.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@
33

44
namespace Microsoft.AspNet.Mvc.ModelBinding
55
{
6+
/// <summary>
7+
/// An entry in a <see cref="ModelStateDictionary"/>.
8+
/// </summary>
69
public class ModelState
710
{
8-
private readonly ModelErrorCollection _errors = new ModelErrorCollection();
11+
/// <summary>
12+
/// Gets the raw value from the request associated with this entry.
13+
/// </summary>
14+
public object RawValue { get; set; }
915

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

12-
public ModelErrorCollection Errors
13-
{
14-
get { return _errors; }
15-
}
21+
/// <summary>
22+
/// Gets the <see cref="ModelErrorCollection"/> for this entry.
23+
/// </summary>
24+
public ModelErrorCollection Errors { get; } = new ModelErrorCollection();
1625

26+
/// <summary>
27+
/// Gets or sets the <see cref="ModelValidationState"/> for this entry.
28+
/// </summary>
1729
public ModelValidationState ValidationState { get; set; }
1830
}
1931
}

src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelStateDictionary.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.Linq;
89
using Microsoft.AspNet.Mvc.Abstractions;
910
using Microsoft.Framework.Internal;
@@ -213,6 +214,7 @@ public bool TryAddModelError([NotNull] string key, [NotNull] Exception exception
213214
// Convert FormatExceptions to Invalid value messages.
214215
ModelState modelState;
215216
TryGetValue(key, out modelState);
217+
216218
string errorMessage;
217219
if (modelState == null)
218220
{
@@ -221,7 +223,7 @@ public bool TryAddModelError([NotNull] string key, [NotNull] Exception exception
221223
else
222224
{
223225
errorMessage = Resources.FormatModelError_InvalidValue_MessageWithModelValue(
224-
modelState.Value.AttemptedValue,
226+
modelState.AttemptedValue,
225227
key);
226228
}
227229

@@ -359,14 +361,50 @@ public void Merge(ModelStateDictionary dictionary)
359361
}
360362

361363
/// <summary>
362-
/// Sets the value for the <see cref="ModelState"/> with the specified <paramref name="key"/> to the
363-
/// specified <paramref name="value"/>.
364+
/// Sets the of <see cref="ModelState.RawValue"/> and <see cref="ModelState.AttemptedValue"/> for
365+
/// the <see cref="ModelState"/> with the specified <paramref name="key"/>.
364366
/// </summary>
365367
/// <param name="key">The key for the <see cref="ModelState"/> entry.</param>
366-
/// <param name="value">The value to assign.</param>
367-
public void SetModelValue([NotNull] string key, [NotNull] ValueProviderResult value)
368+
/// <param name="rawvalue">The raw value for the <see cref="ModelState"/> entry.</param>
369+
/// <param name="attemptedValue">
370+
/// The values of <param name="rawValue"/> in a comma-separated <see cref="string"/>.
371+
/// </param>
372+
public void SetModelValue([NotNull] string key, object rawValue, string attemptedValue)
368373
{
369-
GetModelStateForKey(key).Value = value;
374+
var modelState = GetModelStateForKey(key);
375+
modelState.RawValue = rawValue;
376+
modelState.AttemptedValue = attemptedValue;
377+
}
378+
379+
/// <summary>
380+
/// Sets the value for the <see cref="ModelState"/> with the specified <paramref name="key"/>.
381+
/// </summary>
382+
/// <param name="key">The key for the <see cref="ModelState"/> entry</param>
383+
/// <param name="valueProviderResult">
384+
/// A <see cref="ValueProviderResult"/> with data for the <see cref="ModelState"/> entry.
385+
/// </param>
386+
public void SetModelValue([NotNull] string key, ValueProviderResult valueProviderResult)
387+
{
388+
// Avoid creating a new array for rawvalue if there's only one value.
389+
object rawValue;
390+
if (valueProviderResult == ValueProviderResult.None)
391+
{
392+
rawValue = null;
393+
}
394+
else if (valueProviderResult.Value != null)
395+
{
396+
rawValue = valueProviderResult.Value;
397+
}
398+
else if (valueProviderResult.Length == 1)
399+
{
400+
rawValue = valueProviderResult.Values[0];
401+
}
402+
else
403+
{
404+
rawValue = valueProviderResult.Values;
405+
}
406+
407+
SetModelValue(key, rawValue, (string)valueProviderResult);
370408
}
371409

372410
/// <summary>

0 commit comments

Comments
 (0)