通过ajax传递数据 - 参数字典包含参数的空条目

时间:2015-04-22 00:31:54

标签: javascript c# ajax json asp.net-mvc

我的上一个问题完全相同。 Passing data from ajax to mvc controller - null parameter error

我们在界面和javascript之后改变了一些东西。所以现在当我尝试在服务器上传递数据时,我得到了这个错误

  

参数字典包含参数'方法'的空条目。   非可空类型' API.PVT.Solver.Methods'方法   ' System.Web.Mvc.ActionResult   StandartModelResult(API.PVT.Solver.InputDataModel,   API.PVT.Solver.Methods,API.PVT.Solver.DefaultConstants)'在   ' WebServices.Controllers.PVT.PVTMethodsController&#39 ;.可选的   参数必须是引用类型,可空类型或声明为   可选参数

这是我的客户方请求

 GetServerData:function(inputData) {
        var outputData;
        $.ajax({
            type: 'POST',
            url: "/PVTMethods/StandartModelResult",
            data: JSON.stringify(inputData),
            success: function (data) {
                outputData = data.result;
                appl.PlotData = data.result;
            },
            error: function (data) {
                console.log("Error" + data);
            },
            dataType: "json",
            contentType: "appllication/json; charset=utf-8",
        });
        return outputData;

    }

enter image description here

这里是json的样子:

"{"method":"eMcCain","model":{"task":"eBw","temperatureRanges":{"Start":200,"End":1000,"Samples":30}},"constants":{"ProducedGOR":500,"OilGravity":35,"SeparatorGasGravity":0.7,"StockTankOilGravity":0.84985,"WaterSalinity":20}}"

此处的服务器端型号

  public class DefaultRanges
    {
        public virtual double Start { get; set; } //начало отрезка
        public virtual double End { get; set; } //конец отрезка
        public virtual double Samples { get; set; } //шаги
    }
    //константы
    public class DefaultConstants
    {
        public virtual double ProducedGOR { get; set; }
        public virtual double OilGravity { get; set; }
        public virtual double SeparatorGasGravity { get; set; }
        public virtual double StockTankOilGravity { get; set; }
        public virtual double WaterSalinity { get; set; }
    }

    //pressure rate (pisa)
    public class PressureDefaultRanges : DefaultRanges { }

    //temperature ranges (Фаренгейты если чо)
    public class TemperatureDefaultRanges : DefaultRanges { }

    public class PlotRanges : DefaultRanges
    {
        public virtual double PlotConstant { get; set; } //GOR или Pressure
    }
    // w\ pressure
    public class PlotRangesWP : PlotRanges
    {
        public virtual double Pressure { get; set; }
    }
    // w\ GOR
    public class PlotRangesWGOR : PlotRanges
    {
        public virtual double GOR { get; set; }
    }

    //решаемые задачи
    public enum Tasks { ePb, eRs, eBo, eBg, eMo, eMg, eMod, eBw, eMw, eRsw };
    //применяемые методы
    public enum Methods
    {
        eAbdulMajeed,//+
        eAlShammasi,//+
        eAlKhafaji,//+
        eAlMarhoun,//+
        eAlMarhounSA,//+
        eAlmehaideb,//+
        eAlNajjaretal,/
        eAziz,//+
        eBeal,//+
        eBeggsRobinson,//+
        eBeggsVazquez,//+
        eBergman,//+
        eDempsey,//+
        eDindorukChristman,//+
        eDoklaOsman,//+
        eElam,//+
        eElsharkawy,
        eFarshad,//+
        eGlaso,//+
        eHasan,//+
        eKartoatmodjoSchmidt,//+
        eKhairy,//+
        eKhanEtAl,//dead oil viscosity пока не нужен
        eLabediL,//+
        eLabediNA,//+
        eLasater,//+
        eLeeGonzales,//+
        eLevitanMurtha,//+
        eLucas,//+
        eMcCain,//+
        eMakary,//+
        eObomanuOkpobiri,
        eOmarTodd,//+
        eOwolabiCI,//+
        eOwolabiNS,//+
        ePetrosky,//+
        eSchlumbergerKuwait,//?  
        eStanding//+
    };




    public class InputDataModel
    {

        public Tasks task { get; set; }

        public PressureDefaultRanges pressureRanges { get; set; }

        public TemperatureDefaultRanges temperatureRanges { get; set; }

        public PlotRanges currentTaskPlotRanges { get; set; }
        public double plotConstant { get; set; } 
    }

此处的服务器端控制器方法

 public ActionResult StandartModelResult(InputDataModel model , Methods method, DefaultConstants constants = null)
        {
            SolverInit(constants);

            SetDeafultSettings(); //стандартные настройки методов
            //var method = Methods.eMcCain;
            PVTMethodsSolver.PVTSolverTaskMethodSettings(model.task, method);

            var currentResult = resultArray(model);

            return Json(new { result = currentResult, status = "ok"}, JsonRequestBehavior.AllowGet);

        }

这里有什么问题? 它现在甚至无法到达服务器,我的意思是请求。什么可以帮助?

1 个答案:

答案 0 :(得分:1)

更改类似的ajax代码,它开始工作

GetServerData:function(inputData) {
        var outputData;
        $.ajax({
            type: 'POST',
            async: false,
            url: "/PVTMethods/StandartModelResult",
            data: inputData,
           // async: true,
            success: function (data) {
                outputData = data.result;
                appl.PlotData = data.result;
            },
            error: function (data) {
                console.log("Error" + data);
            },
            dataType: "json",
          //  contentType: "appllication/json; charset=utf-8"
        });
        return outputData;

    }