The following statement:
<%= Html.EditorFor(model => model.Material.Serial) %>
Generates the following code;
<div class="editor-field">
<input type="text" value="" name="Material.Serial" id="Material_Serial" class="input-validation-error">
<span id="Material_Serial_validationMessage" class="field-validation-error">Debe ingresar un serial</span>
</div>
I want to add an onkeypress
javascript attribute to my input through the EditorFor
statement, I have tried to do the following:
<%= Html.EditorFor(model => model.Material.Serial, new{ onkeypress = "return disableEnterKey(event)"})%>
But this doesn't work.
The following statement:
<%= Html.EditorFor(model => model.Material.Serial) %>
Generates the following code;
<div class="editor-field">
<input type="text" value="" name="Material.Serial" id="Material_Serial" class="input-validation-error">
<span id="Material_Serial_validationMessage" class="field-validation-error">Debe ingresar un serial</span>
</div>
I want to add an onkeypress
javascript attribute to my input through the EditorFor
statement, I have tried to do the following:
<%= Html.EditorFor(model => model.Material.Serial, new{ onkeypress = "return disableEnterKey(event)"})%>
But this doesn't work.
Share Improve this question edited Oct 4, 2010 at 19:43 Bertrand Marron 22.2k8 gold badges62 silver badges95 bronze badges asked Oct 4, 2010 at 19:37 josethernandezcjosethernandezc 1602 silver badges12 bronze badges2 Answers
Reset to default 8Its difficult to pass Additional attributes to EditorFor templates, but alternatively the same functionality could be implemented using jQuery or something similar:
$('#Material_Serial').keypress(function(event) {
disableEnterKey(event);
});
Use the following .... note the last part in the code.
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @onchange="alert('Hello World')" } })
HTML generated for the above will be
<input class="form-control text-box single-line" data-val="true" data-val-length="The field Title must be a string with a maximum length of 255." data-val-length-max="255" data-val-required="The Title field is required." id="Title" name="Title" onchange="alert('Hello World')" type="text" value="" />