Skip to content

Follow-ups to 'Fix MVC form data binding localization' (#43182) #43309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Mvc/Mvc.Core/src/ModelBinding/FormValueHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Mvc.ModelBinding;

internal static class FormValueHelper
{
public const string CultureInvariantFieldName = "__Invariant";
}
4 changes: 1 addition & 3 deletions src/Mvc/Mvc.Core/src/ModelBinding/FormValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding;
/// </summary>
public class FormValueProvider : BindingSourceValueProvider, IEnumerableValueProvider
{
internal const string CultureInvariantFieldName = "__Invariant";

private readonly IFormCollection _values;
private readonly HashSet<string?>? _invariantValueKeys;
private PrefixContainer? _prefixContainer;
Expand Down Expand Up @@ -43,7 +41,7 @@ public FormValueProvider(

_values = values;

if (_values.TryGetValue(CultureInvariantFieldName, out var invariantKeys) && invariantKeys.Count > 0)
if (_values.TryGetValue(FormValueHelper.CultureInvariantFieldName, out var invariantKeys) && invariantKeys.Count > 0)
{
_invariantValueKeys = new(invariantKeys, StringComparer.OrdinalIgnoreCase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public JQueryFormValueProvider(
CultureInfo? culture)
: base(bindingSource, values, culture)
{
if (values.TryGetValue(FormValueProvider.CultureInvariantFieldName, out var invariantKeys) && invariantKeys.Count > 0)
if (values.TryGetValue(FormValueHelper.CultureInvariantFieldName, out var invariantKeys) && invariantKeys.Count > 0)
{
_invariantValueKeys = new(invariantKeys, StringComparer.OrdinalIgnoreCase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void GetValue_ReturnsInvariantCulture_IfInvariantEntryExists()
var currentCultureKey = "some";
var values = new Dictionary<string, StringValues>(BackingStore)
{
{ FormValueProvider.CultureInvariantFieldName, new(invariantCultureKey) },
{ FormValueHelper.CultureInvariantFieldName, new(invariantCultureKey) },
};
var valueProvider = GetEnumerableValueProvider(BindingSource.Query, values, culture);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void GetValue_ReturnsInvariantCulture_IfInvariantEntryExists()
var currentCultureKey = "some";
var values = new Dictionary<string, StringValues>(BackingStore)
{
{ FormValueProvider.CultureInvariantFieldName, new(invariantCultureKey) },
{ FormValueHelper.CultureInvariantFieldName, new(invariantCultureKey) },
};
var valueProvider = GetEnumerableValueProvider(BindingSource.Query, values, culture);

Expand Down
2 changes: 1 addition & 1 deletion src/Mvc/Mvc.TagHelpers/src/InputTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private TagBuilder GenerateTextBox(
private static void GenerateInvariantCultureMetadata(string propertyName, TagHelperContent builder)
=> builder
.AppendHtml("<input name=\"")
.Append(FormValueProvider.CultureInvariantFieldName)
.Append(FormValueHelper.CultureInvariantFieldName)
.AppendHtml("\" type=\"hidden\" value=\"")
.Append(propertyName)
.AppendHtml("\" />");
Expand Down
11 changes: 5 additions & 6 deletions src/Mvc/Mvc.ViewFeatures/src/DefaultHtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,13 +1351,12 @@ protected virtual TagBuilder GenerateInput(
string.Equals(suppliedTypeString, "image", StringComparison.OrdinalIgnoreCase))
{
// 'value' attribute is not needed for 'file' and 'image' input types.
break;
}
else
{
var attributeValue = (string)GetModelStateValue(viewContext, fullName, typeof(string));
attributeValue ??= useViewData ? EvalString(viewContext, expression, format) : valueParameter;
tagBuilder.MergeAttribute("value", attributeValue, replaceExisting: isExplicitValue);
}

var attributeValue = (string)GetModelStateValue(viewContext, fullName, typeof(string));
attributeValue ??= useViewData ? EvalString(viewContext, expression, format) : valueParameter;
tagBuilder.MergeAttribute("value", attributeValue, replaceExisting: isExplicitValue);

break;
}
Expand Down