ASP.NET MVC 4 overrides html name and id

I am trying to change the emitted html login name created by @Html.HiddenFor .

The code I'm using is:

 @Html.HiddenFor(e => e.SomeProperty, new { @id = "some_property", @name = "some_property" } 

Now this works for id , however it does not work for name. Now I do not need id anyway, I need to change name , because the one that is sent back to the target server.

Does it exist

  • A property that I can apply on SomeProperty in my model?
  • A way in Html.HiddenFor override the name property?

Or am I stuck to do the usual <input ...> manually?

+42
asp.net-mvc asp.net-mvc-4 razor-2
Feb 11 '13 at 10:18
source share
3 answers

You need to use Html.Hidden (or write <input ...> manually) instead of Html.HiddenFor

 @Html.Hidden("some_property", Model.SomeProperty, new { @id = "some_property" }) 

The goal of strongly typed helpers (for example, a name that ends with the name "For", like HiddenFor ) is to guess the input name for you from the provided expression. Therefore, if you want to have a "custom" input name, you can always use ordinary helpers, such as Html.Hidden , where you can explicitly specify a name.

The accepted answer from unjuken is incorrect because it generates invalid HTML.

Using this solution generates TWO attributes strong> name :

 <input Name="some_property" name="SomeProperty" id="some_property" type="hidden" value="test" /> 

So you will have Name="some_property" AND name="SomeProperty" , which is an INVALID HTML, because the input can only contain the ONE name attribute! (although most browsers take the first Name="some_property" and don't care about the second ...)

+89
Feb 11 '13 at 10:22
source share

If you use:

@ Html.HiddenFor (e => e.SomeProperty, new {@id = "some_property", @Name = "some_property"});

Note the "N" in @Name. He will work.

+33
Jun 05 '13 at 13:26
source share

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.

+1
Feb 22 '17 at 17:51 on
source share



All Articles