-1

I have TextboxFor in partial view,I want to display decimal format example( 3,108,000.00). is work for display but want i call controller by Ajax value is be null. thank for help.

@Html.TextBoxFor(x => x.List_TRNQuotationLeasing.CashBook, new { @class = "numeric form-control right", @Value = Model.List_TRNQuotationLeasing.CashBook != null ? Model.List_TRNQuotationLeasing.CashBook.Value.ToString("#,##0.00") : "" })

enter image description here

enter image description here

D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • Could you put your Ajax' code? – D-Shih Feb 09 '18 at 08:24
  • Can you submit the value as smallest value and then process it server side? So for instance if the value was in GBP and it was 3,108,000.00, you would submit 310800000 - which is `3,108,000.00 * 100` to put it in pence. You would then divide that value on the server by 100 to get the original value – dan richardson Feb 09 '18 at 09:20
  • Otherwise you could go down the custom model binder route - which may be a bit overkill - https://stackoverflow.com/questions/37389084/send-decimal-number-with-commas-working-on-c-sharp-mvc – dan richardson Feb 09 '18 at 09:21

1 Answers1

0

You cannot post a value like 900,000.00 directly to decimal, if you're trying to do that. You need to reformat the number and get rid of the , before sending, use string in the ViewModel and apply convertion after posting, or use a special ASP.NET MVC Model Binder to do that conversion during the Request.

If you want to go with Option 1 for example, you could do this in JavaScript just before sending (jQuery):

 // ...before send...
 // e.g. apply to all fields marked with "numeric"
 $("input.numeric").each(function() {
    numericWithOutComma = $(this).val().replace(",", "");

    // override text... yes it will lose the format. You could enhance that later
    $(this).val(numericWithOutComma);
 });

 // Execute Ajax send...

Also, there is this interesting discussion about that if you want to go with Option 3, for example.

thmshd
  • 5,729
  • 3
  • 39
  • 67