简体   繁体   English

HTML.Editor用于将.textbox添加到元素名称

[英]Html.EditorFor adding .textbox to element name

I have a project which contains a simple form for collecting signup info. 我有一个项目,其中包含用于收集注册信息的简单表单。 Recently I have been working to add localization to the project, as all of the text shown to the user was hardcoded. 最近,我一直在努力为项目添加本地化,​​因为显示给用户的所有文本都是硬编码的。 I'm not sure what changed, but for some reason, now when Razor renders an HTML element using the Html.EditorFor method that ends up being a textbox, the Name property of the element has ".textbox" appended to it. 我不确定发生了什么变化,但是由于某种原因,现在Razor使用Html.EditorFor方法渲染HTML元素(最终成为文本框)时,该元素的Name属性将附加“ .textbox”。

This breaks the bindings, so that when I receive my model all of the text values are null. 这破坏了绑定,因此当我收到模型时,所有文本值都为空。 Here is an example of what I'm seeing, Razor code: 这是我看到的一个示例,Razor代码:

<div class="form-group" ng-class="{ 'has-error': validate && accountForm.FirstName.$invalid }">
        @Html.LabelFor(m => m.FirstName, new { @class = @ViewBag.LabelCssRequired })
        <div class="@ViewBag.TextboxCss">
            @Html.EditorFor(m => m.FirstName, new { htmlAttributes = new { ng_model = "firstName" } })
        </div>
    </div>

and here is the rendered output: 这是渲染的输出:

<input class="text-box single-line form-control ng-valid-maxlength ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" id="FirstName_textbox" maxlength="100" name="FirstName.textbox" ng-model="firstName" required="required" type="text" value="">

It is also adding a "_textbox" to the id, but I'm not as concerned about that at the moment. 它还在id上添加了“ _textbox”,但我现在对此并不在意。 For some reason, this only seems to be happening to input elements where the type is "text". 由于某种原因,这似乎只发生在类型为“文本”的输入元素上。 I have another input generated with.EditorFor which has the type of email and it doesn't have any modifications to the name. 我使用.EditorFor生成了另一个输入,该输入具有电子邮件的类型,并且对该名称没有任何修改。

This behavior also seems to be restricted to Html.EditorFor, if I use.TextboxFor, it works fine. 这种行为似乎也仅限于Html.EditorFor,如果我使用.TextboxFor,它可以正常工作。

I have been able to make the bindings work by explicitly setting the @Name property in Razor, but this only masks the symptom, and I would like to avoid having to do this for every text input on the site. 我已经能够通过在Razor中显式设置@Name属性来使绑定起作用,但这仅掩盖了症状,我想避免必须对网站上的每个文本输入都执行此操作。

Has anyone seen this behavior before, or know of a fix? 有没有人以前见过这种行为,或者知道解决方法?

By default, the TextBoxFor helper generates HTML using a built-in template. 默认情况下, TextBoxFor帮助程序使用内置模板生成HTML。 You can override the defaults by creating files in the project root\\views\\shared\\editortemplates folder. 您可以通过在project root\\views\\shared\\editortemplates文件夹中创建文件来覆盖默认值。

Therefore the problem can be caused by some custom template being present there. 因此,此问题可能是由那里存在一些自定义模板引起的。 Normally, you need to check for files whose name Match either the datatype (such as string ) or the control type (such as TextArea ). 通常,您需要检查名称与数据类型(例如string )或控件类型(例如TextArea )匹配的文件。 If the corresponding model property has a UIHint attribute on it, a custom file specified in it can also come into play. 如果相应的模型属性具有UIHint属性,则其中指定的自定义文件也可以起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM