繁体   English   中英

ASP.NET MVC EditorFor自定义javascript

[英]ASP.NET MVC EditorFor custom javascript

以下声明:

<%= Html.EditorFor(model => model.Material.Serial) %>

生成以下代码;

<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>

我想通过EditorFor语句向我的输入添加onkeypress javascript属性,我尝试执行以下操作:

<%= Html.EditorFor(model => model.Material.Serial, new{ onkeypress = "return disableEnterKey(event)"})%>

但这不起作用。

很难将附加属性传递给EditorFor模板,但也可以使用jQuery或类似的东西实现相同的功能:

$('#Material_Serial').keypress(function(event) {

  disableEnterKey(event);

});

使用以下....注意代码中的最后一部分。

@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @onchange="alert('Hello World')" } })

为上面生成的HTML将是

<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="" />

暂无
暂无

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

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