我的插入方法不起作用

时间:2018-06-23 21:02:53

标签: javascript c# dapper

我正在使用dapper将我的数据库映射到我的mvc项目,我调试了代码以了解发生了什么,并且我的ReceiveData方法在将数据发送到GeoCreate时由于某种原因未接收到数据,因此未显示任何内容,也可能我错了,因为我将光标放在了Geo.RouteID,Geo.Latitude等上,并且数据在那里,然后转到了我的地理类,然后应该转到if语句,但是它在那里停止了,我正在与邮递员发送json数据。

这是我的ReceiveData方法

public bool ReceiveData(Geography Geo)
{
    int GeoCreate = this.conn.Execute(@"INSERT GEOGRAPHY([ID_ROUTE],[LAT],[LONG],[BATTERY],[DateCreated],[DEVICE_ID]) values (@Lat,@Long,@Battery,@DateCreated,@Device_ID)",
                    new { RouteID = Geo.RouteID, Lat = Geo.Latitude, Long = Geo.Longitude, Battery = Geo.Battery, DateCreated = Geo.ObtainedDate, DeviceID = Geo.Device_ID });

    if(GeoCreate > 0)
    {
        return true;
    }
    return false;

}

以下是调用ReceiveData方法的操作:

[Route("GeoCreate")]
[HttpPost]
public bool StoreLocation(Geography Geo)
{
    return GD.ReceiveData(Geo);
}

以下是可用于发送数据的javascript代码:

<script>
    $(document).ready(function () {
        var Geograph = {
            Latitude: $("#Latitude").val(),
            Longitude: $("#Longitude").val(),
            Country: $("#Country").val(),
            ObtainedDate: $("#ObtainedDate").val()
        };
        $.ajax({
            type: 'POST',
            url: '@Url.Action("StoreLocation", "Home")',
            dataType: 'json',
            data: JSON.parse( { Geo: Geograph }),
            success: function (lctn) {
                console.log(lctn);
                debugger;
            }
        });
    });
</script>

我的地理课:

public class Geography
{
    [Key]
    public int RouteID { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

    public int Battery { get; set; }

    public DateTime ObtainedDate { get; set; }

    public int Device_ID { get; set; }

}

这是我要发送的JSON数据:

{
    "Latitude": 19.435547,
    "Longitude": -81.77856,
    "Battery": 100,
    "ObtainedDate":"\/Date(1522600449000)\/",
    "Device_ID": 1
}

我没有设置RouteID,因为它会自动填充到数据库中。

1 个答案:

答案 0 :(得分:2)

在您的 ajax 调用中,您应该使用JSON.stringify而不是parse方法

    $.ajax({
            type: 'POST',
            url: '@Url.Action("StoreLocation", "Home")',
            dataType: 'json',
            data: JSON.stringify( { Geo: Geograph }),
            success: function (lctn) {
                console.log(lctn);
                debugger;

            }
    });
相关问题