asp.net-mvc ASP.NET MVC 4 覆盖发出的 html 名称和 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14810100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 4 override emitted html name and id
提问by Snake
I'm trying to change the emitted name of the html input created by @Html.HiddenFor.
我正在尝试更改由@Html.HiddenFor.
The code I'm using this:
我正在使用的代码:
@Html.HiddenFor(e => e.SomeProperty, new { @id = "some_property", @name = "some_property" }
Now this works for the id, however it doesn't work for the name. Now I don't really care for the idnow, I need the nameto change, because that's the one that get's posted back to the target server.
现在这适用于id,但它不适用于名称。现在我真的不在乎id现在,我需要name改变,因为那是被回发到目标服务器的那个。
Is there
在那儿
- A property I can apply on
SomePropertyin my model? - A way in the
Html.HiddenForto override thenameproperty?
- 我可以
SomeProperty在模型中应用的属性? - 在A方式
Html.HiddenFor覆盖name的财产?
Or am I stuck to do a plain <input ...>by hand?
还是我坚持<input ...>手工做一个平原?
回答by nemesv
You need to use the Html.Hidden(or write out the <input ...>by hand) instead of the Html.HiddenFor
您需要使用Html.Hidden(或<input ...>手动写出)而不是Html.HiddenFor
@Html.Hidden("some_property", Model.SomeProperty, new { @id = "some_property" })
The goal of the strongly typed helpers (e.g the one which the name end "For" like HiddenFor) is to guess the input name for you from the provided expression. So if you want to have a "custom" input name you can always use the regular helpers like Html.Hiddenwhere you can explicitly set the name.
强类型助手(例如名称以“For” like 结尾的助手HiddenFor)的目标是从提供的表达式中为您猜测输入名称。因此,如果您想要一个“自定义”输入名称,您始终可以使用常规助手,例如Html.Hidden您可以明确设置名称的地方。
The answer from unjukenis wrongbecause it generates invalidHTML.
Using that solution generates TWOnameattributes:
使用该解决方案会生成两个name属性:
<input Name="some_property" name="SomeProperty" id="some_property" type="hidden" value="test" />
So you will have Name="some_property"ANDname="SomeProperty"which is INVALIDHTML because an input can only have ONEnameattribute! (although most browers happen to take the first Name="some_property"and don't care about the second one...)
所以你会有Name="some_property"AND ,name="SomeProperty"它是INVALIDHTML 因为一个输入只能有一个name属性!(虽然大多数浏览器碰巧选择了第一个Name="some_property"而不关心第二个......)
回答by unjuken
If you use:
如果您使用:
@Html.HiddenFor(e => e.SomeProperty, new { @id = "some_property", @Name = "some_property" });
@Html.HiddenFor(e => e.SomeProperty, new { @id = "some_property", @Name = "some_property" });
Notice the capital "N" in @Name. It′ll work.
注意@Name 中的大写字母“N”。它会起作用的。
回答by sovemp
I was curious as to why specifically overriding the name attribute wouldn't work. Unless I capitalized it (i.e. new {@Name = 'somename'}), then it doesn't seem to work. As others have pointed out, this only works because it generates duplicated name attributes and Chrome cleans it up.
我很好奇为什么专门覆盖 name 属性不起作用。除非我将它大写(即new {@Name = 'somename'}),否则它似乎不起作用。正如其他人指出的那样,这只是因为它会生成重复的名称属性并且 Chrome 会对其进行清理。
I looked at the latest MVC source code to figure out what is going on. Consider the following snippet from the GenerateInputmethod in DefaultHtmlGenerator.cs:
我查看了最新的 MVC 源代码以弄清楚发生了什么。考虑以下GenerateInput方法中的片段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, regardless of whatever name property you provide, it will be overridden by the last call to MergeAttribute, which will use whatever logic it is that assigns to the variable fullNamefrom the GetFullHtmlFieldNamemethod.
我们可以在这里看到,问题在于,无论您提供什么 name 属性,它都会被最后一次调用覆盖MergeAttribute,它将使用fullName从GetFullHtmlFieldName方法分配给变量的任何逻辑。
I sort of understand why they enforce this behavior, guessing it has something to do with controlling the names used in the postback to guarantee it works with the model binder.
我有点理解他们为什么强制执行这种行为,猜测它与控制回发中使用的名称以确保它与模型绑定器一起工作有关。
In any case, to make this happen, I say just manually construct the input element and don't use the razor view helper.
在任何情况下,为了实现这一点,我说只需手动构建输入元素,不要使用 razor 视图助手。
回答by Carl Verret
never worked for me (aspnet.core)
从来没有对我来说有效过(aspnet.core)
I used plain
我用普通
<input type="hidden" id="@myid" name="@myname" value="@Model.prop" />
and worked like a charm. No need for HtmlHelper HiddenForModel.
并像魅力一样工作。不需要 HtmlHelper HiddenForModel。

