I was curious why exactly overriding the name attribute would not work. If I have not used it (i.e. new {@Name = 'somename'} ), then it does not seem to work. As others have pointed out, this only works because it generates duplicate name attributes, and Chrome clears it.
I looked at the latest MVC source code to find out what was going on. Consider the following snipper from the GenerateInput method in DefaultHtmlGenerator.cs :
var fullName = NameAndIdProvider.GetFullHtmlFieldName(viewContext, expression); if (string.IsNullOrEmpty(fullName)) { throw new ArgumentException( ... } var inputTypeString = GetInputTypeString(inputType); var tagBuilder = new TagBuilder("input"); tagBuilder.TagRenderMode = TagRenderMode.SelfClosing; tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("type", inputTypeString); tagBuilder.MergeAttribute("name", fullName, replaceExisting: true);
We can see here, the problem is that no matter what name property you provide, it will be undone by the last call to MergeAttribute , which will use any logic that assigns the fullName variable from GetFullHtmlFieldName .
I understand why they apply this behavior, assuming it has something to do with managing the names used in the postback to ensure that it works with model binding.
In any case, for this to happen, I simply manually create an input element and do not use the helper view.
sovemp Feb 22 '17 at 17:51 on 2017-02-22 17:51
source share