これが検証されるモデルです。
public class EstimateItem
{
public string ItemName { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
[DataType(DataType.Currency)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n2}")]
public decimal PerUnitCost { get; set; }
public string PerUnitDescription { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n0}")]
[Min(1, ErrorMessage = "Cannot be zero")]
public int Units { get; set; }
public string UnitsDescription { get; set; }
public bool IsEnabled { get; set; }
public bool IsBasedOnHomeSquareFootage { get; set; }
[DataType(DataType.Currency)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c2}")]
public decimal Cost { get; set; }
public List<EstimateItemOption> Options { get; set; }
public decimal ItemTotal { get; set; }
}
これは、モデルを埋めるテキスト ボックスです。
@Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitCost,
new { @disabled = "disabled", @onchange = "costUpdate(this);", @cstType = "perUnit" })
これは、@onChange が呼び出されたときに処理される JavaScript です。
case 'perUnit':
var it = $(src).closest('.itemRow').find("input[cstType='cost']");
var gt = parseFloat(et.html().replace('$', '').replace(',', ''));
et.html(gt - parseFloat(it.val()));
if ($(src).closest('.itemRow').find('[hs]').attr('hs') == 'True') {
var nv = $(src).val();
if (nv == null || nv.length == 0) nv = 0;
it.val(Math.round(nv * parseFloat($('#SquareFootage').val()) * 100) / 100)
$(src).closest('.itemRow').find('.hcst').val(it.val());
et.html(CurrencyFormatted(parseFloat(et.html()) + parseFloat(it.val())));
} else {
var nv = $(src).val();
if (nv == null || nv.length == 0) nv = 0;
it.val(Math.round(nv * parseFloat($(src).closest('.itemRow').find("input[cstType='units']").val()) * 100) / 100)
$(src).closest('.itemRow').find('.hcst').val(it.val());
et.html(CurrencyFormatted(parseFloat(et.html()) + parseFloat(it.val())));
}
break;
テキスト ボックスで .5 のように 1 未満の小数を使用できない理由を知る必要があります。