Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 发布数据时,Json integer将转换字符串。Net核心+;反应_.net_Json_Reactjs_String_Api - Fatal编程技术网

.net 发布数据时,Json integer将转换字符串。Net核心+;反应

.net 发布数据时,Json integer将转换字符串。Net核心+;反应,.net,json,reactjs,string,api,.net,Json,Reactjs,String,Api,我试图将Car对象添加到我的数据库中,只使用字符串就可以了,但是当我尝试添加一个整数值时,该值在Json中被引用,因此变成了字符串。 以下是我的.Net核心模型: public class Car : Service { public string Description { get; set; } public int Price { get; set; } public string Options { get; set; }

我试图将Car对象添加到我的数据库中,只使用字符串就可以了,但是当我尝试添加一个整数值时,该值在Json中被引用,因此变成了字符串。 以下是我的.Net核心模型:

public class Car : Service
    {
        public string Description { get; set; }
        public int Price { get; set; }
        public string Options { get; set; }
    }
这是我的创建处理程序

 public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var car = new Car
                {
                    id = request.id,
                    Name = request.Name,
                    Description = request.Description,
                    Price = request.Price,
                    Options = request.Options
                };

                _context.Cars.Add(car);
                var success= await _context.SaveChangesAsync() > 0;

                if(success) return Unit.Value;

                throw new System.Exception("Problem saving changes");
            }

请问如何确保值作为整数传递?感谢您的帮助。

今天我发现自己遇到了这个问题

您可以使用
valueAsNumber
以双精度形式获取值

double
按顺序返回元素的值,解释为以下值之一:

  • 时间值
  • NaN
    如果无法转换
来源-

如果您使用一个基本的“onChange”类型函数来处理输入,您可以将其更改为如下内容(如果它不是一个数字,它将像普通一样将其指定为字符串):

或者,如果您需要对转换进行更有限的控制,您可以执行类似于@在中建议的操作:

希望这有帮助

{id: "f8aa6881-8a90-4510-9505-5471c1f9a656", name: "Mercedes AMG", description: "a", price: "800",…}
description: "a"
id: "f8aa6881-8a90-4510-9505-5471c1f9a656"
name: "Mercedes AMG"
options: "a"
price: "800" //This is the value creating the problem
price:{}; The JSON value could not be converted to System.Int32
    onChange = e => this.setState({ [e.target.name]: e.target.valueAsNumber || e.target.value })
this.setState({
  [e.target.name]: e.target.type === 'number' ? parseInt(e.target.value) : e.target.value
});

// or

this.setState({
  [e.target.name]: e.target.name === 'price' ? parseFloat(e.target.value) : e.target.value
});