Asp.net mvc 3 Html属性破坏编辑器模板,使其只读

Asp.net mvc 3 Html属性破坏编辑器模板,使其只读,asp.net-mvc-3,mvc-editor-templates,Asp.net Mvc 3,Mvc Editor Templates,我目前正在从事一个ASP.NETMVC3项目。我制作了一个自定义编辑器模板来显示百分比。这是我到目前为止的代码 public class MyClass { // The datatype can be decimal or double [Percentage] public double MyPercentage { get; set; } } [Percentage]属性是使用以下代码的普通UI提示属性: @model Object @{

我目前正在从事一个ASP.NETMVC3项目。我制作了一个自定义编辑器模板来显示百分比。这是我到目前为止的代码

public class MyClass
{
   // The datatype can be decimal or double
   [Percentage]
   public double MyPercentage { get; set; }
}
[Percentage]属性是使用以下代码的普通UI提示属性:

    @model Object
    @{
        string fieldName = ViewData.TemplateInfo.HtmlFieldPrefix;
    }

    <div style="height: 24px;">
        <div style="float: left;">
          @Html.TextBox("", String.Format("{0:0.00}", Model), new
          {
              id = "txt" + fieldName,
              @Class = "magnaNumericTextBox",
              type = "magnaNumericType",
              style = "width:230px"
          })
          &nbsp;%
        </div>
        <div style="float: left;">
            <ul style="height: 24px; list-style: none; padding: 0px; margin: 0px; line-height: none;">
                <li style="line-height: normal"><a id="btn@(fieldName)Up" class="magnaNumericButton button small">
                    ˄</a> </li>
                <li style="line-height: normal"><a id="btn@(fieldName)Down" class="magnaNumericButton button small">
                    ˅</a> </li>
            </ul>
        </div>
    </div>
    <script type="text/javascript">

        $("#btn@(fieldName)Up").click(function ()
        {
            ChangeNumericUpDownValue($('#txt@(fieldName)'), 1);
            return false;
        });

        $("#btn@(fieldName)Down").click(function ()
        {
            ChangeNumericUpDownValue($('#txt@(fieldName)'), -1);
            return false;
        });

        $('#txt@(fieldName)').keypress(function (e)
        {
            NumericUpDownKeyPress($(this), e);
            return false;
        });

    </script>
用户可以通过在文本框中键入值来再次添加该值。这可能是什么?任何帮助或建议都将不胜感激


谢谢

发生这种情况的原因是您从订阅的
按键
javascript事件返回false:

$('#txt@(fieldName)').keypress(function (e) {
    NumericUpDownKeyPress($(this), e);
    return false; // <- here you are blocking all keys
});
$('#txt@(字段名)')。按键(功能(e){
NumericUpDownKeyPress($(此),e);

return false;//为字段生成id的方式不正确。
ViewData.TemplateInfo.HtmlFieldPrefix
是字段的前缀(例如,[Car.Window.Thank a million.Thank)。我从未猜到这一点,但现在一切都有意义了。我不知道为什么我首先将return false放在那里。
$('#txt@(fieldName)').keypress(function (e) {
    NumericUpDownKeyPress($(this), e);
    return false; // <- here you are blocking all keys
});