Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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
通过jQuery ajax发布实体集合_Jquery_Asp.net_Ajax_Entity Framework - Fatal编程技术网

通过jQuery ajax发布实体集合

通过jQuery ajax发布实体集合,jquery,asp.net,ajax,entity-framework,Jquery,Asp.net,Ajax,Entity Framework,我用的是EF4。目前,我正在通过jQuery ajax POST方法将数据插入两个实体中。 实体看起来像: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the fo

我用的是EF4。目前,我正在通过jQuery ajax POST方法将数据插入两个实体中。 实体看起来像:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AddArea : System.Web.Services.WebService
{
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public Int64 InsertArea(string forestType, byte NumberOfPillars, bool isReferencePoint, decimal AreaCalculated)
    {
        using (var db = new DefaultCS())
        {
            var forestArea = new ForestArea();
            forestArea.ForestType = forestType;
            forestArea.NumberOfPillars = NumberOfPillars;
            forestArea.isReferencePoint = isReferencePoint;
            forestArea.AreaCalculated = AreaCalculated;

            db.ForestAreas.AddObject(forestArea);
            db.SaveChanges();

            return forestArea.Id;
        }
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string InsertCoordinates(long forestAreaId, string pillarNo, string latitude, string longitude, string forwardBearings, string backBearings, decimal backDistanceInMeter, decimal backDistanceInChain)
    {
        using (var db = new DefaultCS())
        {
            ForestCoordinate forestCoordinates = new ForestCoordinate();
            forestCoordinates.ForestAreaId = forestAreaId;
            forestCoordinates.PillarNo = pillarNo;
            forestCoordinates.Latitude = latitude;
            forestCoordinates.Longitude = longitude;
            forestCoordinates.ForwardBearings = forwardBearings;
            forestCoordinates.BackBearings = backBearings;
            forestCoordinates.BackDistanceInMeter = backDistanceInMeter;
            forestCoordinates.BackDistanceInChain = backDistanceInChain;

            db.ForestCoordinates.AddObject(forestCoordinates);
            db.SaveChanges();
        }
        return "true";
    }
}
我的Web服务看起来像:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AddArea : System.Web.Services.WebService
{
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public Int64 InsertArea(string forestType, byte NumberOfPillars, bool isReferencePoint, decimal AreaCalculated)
    {
        using (var db = new DefaultCS())
        {
            var forestArea = new ForestArea();
            forestArea.ForestType = forestType;
            forestArea.NumberOfPillars = NumberOfPillars;
            forestArea.isReferencePoint = isReferencePoint;
            forestArea.AreaCalculated = AreaCalculated;

            db.ForestAreas.AddObject(forestArea);
            db.SaveChanges();

            return forestArea.Id;
        }
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string InsertCoordinates(long forestAreaId, string pillarNo, string latitude, string longitude, string forwardBearings, string backBearings, decimal backDistanceInMeter, decimal backDistanceInChain)
    {
        using (var db = new DefaultCS())
        {
            ForestCoordinate forestCoordinates = new ForestCoordinate();
            forestCoordinates.ForestAreaId = forestAreaId;
            forestCoordinates.PillarNo = pillarNo;
            forestCoordinates.Latitude = latitude;
            forestCoordinates.Longitude = longitude;
            forestCoordinates.ForwardBearings = forwardBearings;
            forestCoordinates.BackBearings = backBearings;
            forestCoordinates.BackDistanceInMeter = backDistanceInMeter;
            forestCoordinates.BackDistanceInChain = backDistanceInChain;

            db.ForestCoordinates.AddObject(forestCoordinates);
            db.SaveChanges();
        }
        return "true";
    }
}
InserArea
方法运行一次,但“InsertCoordinates”在循环中运行。 以下是我的ajax:

$.ajax({
    type: "POST",
    url: "AddArea.asmx/InsertArea",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: '{ "forestType": "R", "NumberOfPillars": "' + (parseInt($('#txtNoPillar').val()) + 1) + '", "isReferencePoint": "' + $('#rdReference').is(':checked') + '", "AreaCalculated": "' + $('#areaM').text() + '" }',
    success: function (response) {
        var areaId = response.d;
        var numOfPillar = $('#txtNoPillar').val();
        var i = 0;
        for (i; i <= numOfPillar; i++) {

            $.ajax({
                type: "POST",
                url: "AddArea.asmx/InsertCoordinates",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: '{ "forestAreaId": "' + areaId + '", "pillarNo": "' + $('#txtPillarNo' + i).val() + '", "latitude": "' + $('#txtLatitude' + i).val() + '", "longitude": "' + $('#txtLongitude' + i).val() + '", "forwardBearings": "' + $('#txtBearing' + i).val() + '", "backBearings": "' + $('#txtBackBearing' + i).val() + '", "backDistanceInMeter": "' + $('#txtDistance' + i).val() + '", "backDistanceInChain": "' + $('#txtDistanceInChain' + i).val() + '" }',
                success: function (response) {
                    console.log(response);
                },
                error: function (response) {
                    console.log(response);
                }
            });
        }
        console.log(response);
    },
    error: function (response) {
        console.log(response);
    }
}).done(function () {
    alert("Data saved successfully.");
});
jQuery是:

var forestArea = {};

                forestArea.forestType = "R";
                forestArea.NumberOfPillars = (parseInt($('#txtNoPillar').val()) + 1);
                forestArea.isReferencePoint = $('#rdReference').is(':checked');
                forestArea.AreaCalculated = $('#areaM').text();

                var json = { "forestCoordinates": [] };
                var numOfPillar = $('#txtNoPillar').val();
                var i = 0;
                for (i; i <= numOfPillar; i++) {

                    json.forestCoordinates.push({ pillarNo: $('#txtPillarNo' + i).val(), latitude: $('#txtLatitude' + i).val(), longitude: $('#txtLongitude' + i).val(), forwardBearings: $('#txtBearing' + i).val(), backBearings: $('#txtBackBearing' + i).val(), backDistanceInMeter: $('#txtDistance' + i).val(), backDistanceInChain: $('#txtDistanceInChain' + i).val() });
                }

                // Create a data transfer object (DTO) with the proper structure.
                var DTO = { 'forestArea': forestArea, 'forestCoordinates': json };
                $.ajax({
                    type: "POST",
                    url: "AddArea.asmx/InsertArea",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(DTO),
                    success: function (response) {
                        var areaId = response.d;
                        console.log(response);
                    },
                    error: function (response) {
                        console.log(response);
                    }
                }).done(function () {
                    alert("Data saved successfully.");
                });
var forestArea={};
forestArea.forestType=“R”;
forestArea.NumberOfPillars=(parseInt($('txtNoPillar').val())+1);
forestArea.isReferencePoint=$('#rdReference')。是(':checked');
forestArea.AreaCalculated=$(“#areaM”).text();
var json={“forestCoordinates”:[]};
var numopillar=$('#txtNoPillar').val();
var i=0;

对于(i;i是的。可以大大减少代码

尽管ASMX已经过时,但它确实支持将集合作为公开操作的参数

快速样本:

服务器端:

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld(Ent[] entities)
        {
            return "Hello World";
        }


        public class Ent
        {
            public string Name { get; set; }    
        }
    }
客户:

$(function () {
            var json = { "entities": [] };
            json.entities.push({ Name: "name1" });
            json.entities.push({ Name: "name2" });
            json.entities.push({ Name: "name3" });
            $.ajax({
                type: "POST",
                url: "/Services/WebService1.asmx/HelloWorld",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data:  JSON.stringify(json),
                success: function(response) {
                    console.log(response);
                },
                error: function(response) {
                    console.log(response);
                }
            });
        });
一些有用的链接:


  • 谢谢@Eugene,但我已经键入了要发布的
    EntityCollection forestCoordinates
    类型。请查看新Web服务的更新。或者我应该发布数组并循环到Web服务中吗?