所以我有一个double
名为Area
. 我这样打印:
<div>@Html.EditorFor(model => model.Area)</div>
我的 Double EditorTemplate 看起来像这样:
@{
var attributes = this.ViewData.ModelMetadata.ContainerType.GetProperty(this.ViewData.ModelMetadata.PropertyName).GetCustomAttributes(true);
var htmlAttributes = GAtec.AgroWeb.Basic.Ux.Web.Helpers.EditorTemplateHelper.GetAllAttributes(ViewData, attributes);
var decimalPlaces = htmlAttributes["data-format"].ToString().Split(',');
var value = decimalPlaces.Length > 1 ? Model.ToString("n" + decimalPlaces[1]) : Model.ToString();
@System.Web.Mvc.Html.InputExtensions.TextBox(this.Html, Html.NameFor(model => model).ToString(), value, htmlAttributes);
}
<script>
$(document).ready(function () {
$("#@Html.IdFor(model => model)").restrictNumber();
});
</script>
很好,但为什么 html 元素出现错误<input id="Area_Area" />
而 jQuery 选择器出现正确$("#Area")
?
当我在调试时观察它们并NameFor
返回IdFor
“区域” 。
更新:
我的htmlAttributes
(正如@DarinDimitrov 所问)返回这个数组:
["data-min": "0.0", "data-format": "0,2"]