带十进制值的asp.net core 2.0绑定模型 [英] asp.net core 2.0 bind model with decimal value

查看:41
本文介绍了带十进制值的asp.net core 2.0绑定模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直在寻找解决方案,但没有找到对我有用的解决方案.

I've been looking for a solution to this case for hours, and I did not find anything that worked out for me.

这里我们使用逗号作为小数点分隔符,当我发送"15,50"之类的值时,在我的控制器中我得到的模型值为"1550",并且只有当我发送"15.50"时才有意义.

Here we use the comma for decimal separator, and when I send a value like "15,50", in my controller I get the value of the model as "1550", and it only makes sense if I send "15.50".

我涵盖了以下主题,但没有任何效果.

I covered the following topics, and nothing worked.

在Asp中设置CultureInfo.网芯有一个.作为CurrencyDecimalSeparator而不是

Aspnet核心十进制绑定不适用于非英语文化

我正在使用 $ form.serializeArray()使用ajax发送表单,如下所示:

I am sending the form with ajax, using the $form.serializeArray() as below:

function getFormData(xform) {
    var $form = $('#' + xform);
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });    
    return indexed_array;
}

发送:

function PostFormAjax(controller, metodo, params, redir, divacao, reloadgrid) {

    if (params != null) {
        var formData = new FormData();

        var json = $.parseJSON(JSON.stringify(params));
        $(json).each(function (i, val) {
            $.each(val, function (k, v) {
                console.log(k + " : " + v);
                formData.append(k, v);
            });
        });
    }

    $.ajax({
        url: '/' + controller + '/' + metodo,
        data: JSON.stringify(params),
        contentType: 'application/json',
        type: 'POST',
        success: function (data) {
            AjaxFim(metodo, data, redir, divacao, reloadgrid);
        }
    });
}

我的控制器:

    [HttpPost]
    public IActionResult GravaProduto([FromBody] Produtos produto)
    {
        if (ModelState.IsValid)
        {
         //produto.Valorcusto is 1550 and not 15,50 as it was posted
         //produto.Valorvenda is 1550 and not 15,50 as it was posted
        }
    }

我的Model.cs

My Model.cs

public partial class Produtos
{
    public int Cod { get; set; }
    public string Descricao { get; set; }
    public decimal Valorcusto { get; set; }
    public decimal Valorvenda { get; set; }
}

我尝试在 formData.append(k,v.replace(',','.')); 中进行替换,但也无法正常工作,我也不知道哪个字段是小数.

I tried to make a replace in formData.append(k, v.replace(',', '.')); and also does not work, I also would not know which field is a decimal.

我该怎么办?因为迷路了,本来应该更简单的事情变得更加复杂.

What do I have to do? because I'm lost, what should have been simpler became more complicated.

在找到解决方案之前,我一直努力工作:

What I did to keep working until I found a solution was:

根据我的区域设置戴口罩:

Wear a mask according to my regional setting:

$('.stValor').mask("#.###.##0,00", { reverse: true });

在发布表单之前,我切换到可接受的格式:

And before posting the form, I switch to the accepted formatting:

$('#fCadAltProd').validator().on('submit', function (e) {
    if (e.isDefaultPrevented()) {
    } else {
        e.preventDefault();
        $('.stValor').mask("#,###,##0.00", { reverse: true });
        //Ajax post here
    }
});

推荐答案

使用此插件

Use this plugin jquery.serializeToJSON and change your getFormData() function to

function getFormData(xform) {
    var $form = $('#' + xform);
    var obj = $form.serializeToJSON({
        parseFloat: {
            condition: ".stValor,.stQnt,.stDesconto",
            nanToZero: true,
            getInputValue: function (i) {
                return i.val().split(".").join("").replace(",", "."); 
            }
        }
    });
    return obj;
}

parseFloat.getInputValue:function(){},默认情况下,返回不带逗号的输入值,转换时不会发生错误.如果您的位置使用逗号进行小数点分隔(例如德语或巴西),则可以更改为

parseFloat.getInputValue: function(){}, By default, returns the input value without commas, not an error occurs in conversion. if your location uses comma for decimal separation, for example in German or Brazil, you can change to

function(i){ 
    return i.val().split(".").join("").replace(",", "."); 
}

这将为您完成所有工作.

This will do all the work for you.

这篇关于带十进制值的asp.net core 2.0绑定模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆