2012-04-17 76 views
0

我有包含這是基於這個 文章關於Model Binding Decimal Values模型綁定請求

這個工程使用時通過一個Ajax請求,除了罰款逗號小數自定義模型粘合劑

它的工作原理罰款不包含用逗號量Ajax請求(即值不足千元)

誤差

System.ArgumentException: The parameters dictionary contains a null entry for parameter 'amount' 
of non-nullable type 'System.Decimal' for method 
'System.Web.Mvc.JsonResult IsDepositRequired(System.String, System.String, System.String, 
    Boolean, System.Decimal, System.DateTime)' in 'Client.Controllers.DealingController'. An 
    optional parameter must be a reference type, a nullable type, or be declared as an 
    optional parameter. 
    Parameter name: parameters 
    at System.Web.Mvc.ActionDescriptor. 
    ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary`2 parameters, MethodInfo methodInfo) 

模型綁定在golbal.asax正確註冊

任何想法,我可以錯過 感謝

Ajax代碼:

$.post('/Dealing/IsDepositRequired', { 
       baseCurrency: deal.baseCurrency, 
       termsCurrency: deal.termsCurrency, 
       dealtCurrency: deal.dealtCurrency, 
       isBuy: deal.direction == 'BUY', 
       amount: deal.dealtCurrency == deal.baseCurrency ? deal.baseAmount : deal.termsAmount, 
       valueDate: deal.valueDate 
      }, function (show) { 
       if (show) { 
        $('.Deposit').fadeIn(500); 
       } else { 
        $('.Deposit').hide(); 
       } 
      }, 'json'); 

控制器

[HttpPost] 
public virtual JsonResult IsDepositRequired(string baseCurrency, string termsCurrency, string dealtCurrency, bool isBuy, decimal amount, DateTime valueDate) 
{ 

螢火蟲網控制檯:

amount   100,000.00 
baseCurrency GBP 
dealtCurrency GBP 
isBuy   true 
termsCurrency EUR 
valueDate   30/10/2012 

來源:

baseCurrency=GBP&termsCurrency=EUR&dealtCurrency=GBP&isBuy=true&amount=100%2C000.00&valueDate=30%2F10%2F2012

+2

將使用模型聯編程序 - 您可能無法正確創建您的AJAX請求。你有沒有嘗試過使用Fiddler或類似的東西來查看實際的請求? – Simon 2012-04-17 13:57:21

+2

你能發佈你的AJAX代碼嗎? – 2012-04-17 13:59:00

+0

我添加了以上代碼 – ChrisCa 2012-04-17 14:32:23

回答

1

你的Ajax調用的動作可能無法在正確的查詢字符串PARAM發送量或形成編碼值。您可以使用提琴手檢查請求以查看發送的內容。

+0

是的,忘記明確指定我實際上發送json – ChrisCa 2012-04-17 14:19:47

+0

,它仍然不工作 – ChrisCa 2012-04-17 14:32:49

0

您最好將它們放入現有的模型/新模型中,並將視圖字段封裝在表單中,然後將表單序列化爲請求。像這樣?

假設這是在你的領域模型:

public class MyModel 
{ 
    public string BaseCurrency { get; set; } 
    public string TermsCurrency { get; set; } 
    public string DealtCurrency { get; set; } 
    public bool IsBuy { get; set; } 
    public decimal Amount { get; set; } 
    public DateTime ValueDate { get; set; } 
} 

然後假設你有一個觀點是這樣的:

@model MyModel 

//putting these fields in a form with an id of myForm 
@Html.EditorFor(m => m.BaseCurrency) 
//rest of your fields... 

然後在你的Ajax調用指定的數據時,你只是發以下爲你的數據:

$("#myForm").serialize() 

然後你的動作方法取模型,li ke如此:

[HttpPost] 
public virtual JsonResult IsDepositRequired(MyModel model)