2013-05-14 17 views
-2

這是獲得驗證的模型。一個數字驗證問題。 .5不是數字,但是0.5是?

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

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; 

,當我需要知道爲什麼文本不會讓我用一個小數點小於1像一個0.5 它說,被處理的JavaScript該perunitcost必須是數字

+0

你能解釋一下JavaScript應該做什麼嗎? 'et'變量代表什麼? – PinnyM 2013-05-14 14:20:48

+0

您是否確定失敗是由C#代碼還是Javascript代碼引起的? – 2013-05-14 14:23:47

+0

什麼是CurrencyFormatted? – epascarello 2013-05-14 14:24:32

回答

0

嘗試使用此方法輸入強制數字:

case 'perUnit': 
    var input = $(src), // may as well cache this as you're using it six times. 
     it = input.closest('.itemRow').find("input[cstType='cost']"), 
     gt = parseFloat(et.html().replace('$', '').replace(',', '')), 
     nv = parseFloat(input.val(), 10); // parse to a float to ensure a number 
    et.html(gt - parseFloat(it.val())); 
    if (!nv) { // nv is now either a number, or NaN, so using truthy/falsy to determine if NaN 
     nv = 0; // technically, 0 is falsy but this sets it to 0 anyway, so not an issue. 
    } 
    if (input.closest('.itemRow').find('[hs]').attr('hs') == 'True') { 
     it.val(Math.round(nv * parseFloat($('#SquareFootage').val()) * 100)/100) 
     input.closest('.itemRow').find('.hcst').val(it.val()); 
     et.html(CurrencyFormatted(parseFloat(et.html()) + parseFloat(it.val()))); 
    } else { 
     it.val(Math.round(nv * parseFloat(input.closest('.itemRow').find("input[cstType='units']").val()) * 100)/100) 
     input.closest('.itemRow').find('.hcst').val(it.val()); 
     et.html(CurrencyFormatted(parseFloat(et.html()) + parseFloat(it.val()))); 
    } 
    break; 
相關問題