最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ASP.NET MVC EditorFor custom javascript - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

Its 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(&#39;Hello World&#39;)" type="text" value="" />
发布评论

评论列表(0)

  1. 暂无评论