我目前正在開發ASP.NET MVC 3項目。我製作了一個自定義編輯器模板來顯示百分比。這是我迄今爲止的代碼。Html屬性中斷編輯器模板,使其只讀
public class MyClass
{
// The datatype can be decimal or double
[Percentage]
public double MyPercentage { get; set; }
}
的[百分比] attribue是一個正常的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"
})
%
</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="[email protected](fieldName)Up" class="magnaNumericButton button small">
˄</a> </li>
<li style="line-height: normal"><a id="[email protected](fieldName)Down" class="magnaNumericButton button small">
˅</a> </li>
</ul>
</div>
</div>
<script type="text/javascript">
$("#[email protected](fieldName)Up").click(function()
{
ChangeNumericUpDownValue($('#[email protected](fieldName)'), 1);
return false;
});
$("#[email protected](fieldName)Down").click(function()
{
ChangeNumericUpDownValue($('#[email protected](fieldName)'), -1);
return false;
});
$('#[email protected](fieldName)').keypress(function (e)
{
NumericUpDownKeyPress($(this), e);
return false;
});
</script>
該編輯模板利用一個數字上下輥的,使得用戶可以,如果他使用/她喜歡。用戶也可以自己輸入數字,而不使用數字向上功能。 JavaScript功能和一切工作完美一段時間,直到昨天。
現在的問題是,編輯器模板中的文本框不允許用戶鍵入他/她自己的值(使其只讀 - 儘管呈現的html中沒有隻讀屬性),只能通過數字向上鍵入鈕釦。我已經決定,如果我從文本框中助手刪除HTML屬性,像這樣:
@Html.TextBox("", String.Format("{0:0.00}", Model))
用戶可以再次通過鍵入到文本框中添加值。這可能是什麼?任何幫助或建議,將不勝感激。
感謝
您爲該字段生成ID的方式不正確。 'ViewData.TemplateInfo.HtmlFieldPrefix'是該字段的前綴(例如[Car.Window。< - HtmlPrefix] Color)。如果你想要一個Id或Name,你應該在模板 – 2012-03-07 13:47:29