简体   繁体   English

使用json和Ajax将对象传递给MVC View

[英]Pass an object to MVC View using json and Ajax

Hi I want to pass an object to my view and work with it in JavaScript. 嗨,我想将一个对象传递给我的视图,并在JavaScript中使用它。 I've created this model 我创造了这个模型

    namespace WebApplication1.Models
{
    public class OpenlayerLayer
    {



        public OpenlayerLayer(string Layers,string name,string url,string style,Boolean isqueryable,string projection,string maxEx)
        {
            LAYERS = Layers;

            Name = name;

            URL = url;
            STYLES = style;
            queryable = isqueryable;
            this.projection = projection;
            maxExtent = maxEx;
        }
        public string LAYERS { get; set; }
        public string Name { get; set; }
        public string URL { get; set; }
        public string STYLES { get; set; }

        public Boolean queryable { get; set; }

        public string projection { get; set; }

        public string maxExtent { get; set; }
    }
}

and I used this method in controller 我在控制器中使用了这个方法

        public string getLayerlist()
    {
        OpenlayerLayer ol = new OpenlayerLayer("Test: GParcel", "Parcels", "http://127.0.0.1:8080/geoserver/test/wms", "", true, "EPSG:900913", "5725524.327803587, 3835467.5859751734, 5729564.058979791, 3841404.792330884");

        var json = JsonConvert.SerializeObject(ol);
        return json;
    }

well, I used this in my view 好吧,我在我看来用过这个

 $(selector).getJSON('getLayerlist', data, loadopenlayers(data, status))

this is 'loadopenlayers' method 这是'loadopenlayers'方法

function loadopenlayers(result,status) {

    alert("Seems works fine");
    var layer = JSON && JSON.parse(result) || $.parseJSON(result);

 var   wms2 = new OpenLayers.Layer.WMS(
 layer.name, layer.URL,
 {
     LAYERS: layer.LAYERS,
     STYLES: layer.STYLES,
     format: format,
 },
 {
     buffer: 0,
     displayOutsideMaxExtent: true,
     isBaseLayer: true,
     projection: new OpenLayers.Projection(layer.projection),
     minResolution: null,
     maxResolution: 48,
     numZoomLevels: 32,
     maxScale: null,
     minScale: 1271428,
     maxExtent: new OpenLayers.Bounds(layer.maxExtent),
     minExtent: null,
     displayProjection: new OpenLayers.Projection("EPSG:4326"),
 }
 );

}

so It must work and then call loadopenlayers in JavaScript code,right? 所以它必须工作,然后在JavaScript代码中调用loadopenlayers ,对吧? Now I have a problem how do I work with result of getLayerlist in loadopenlayers 现在我有一个问题如何在loadopenlayers中使用getLayerlistloadopenlayers

Is my method right way to communicate between models and JavaScript in view? 我的方法是否正确地在模型和JavaScript之间进行通信? In fact I have a large number of JavaScript codes which must be customized using model parameters and I want a consistence and standard method to do it Thanks very much 事实上我有大量的JavaScript代码,必须使用模型参数进行自定义,我想要一致性和标准方法来做到这一点非常感谢

Better use jquery ajax call eg: 更好地使用jquery ajax调用例如:

 $.ajax({
          type: "POST",
          url: '/ControllerName/getLayerlist',
          // data: data,send data to controller from here
          dataType: 'json',
          //processData: false,
          success: function (msg) {
            //your json data will be available in msg
            //here you can also call loadopenplayers() method
          },error: function (error) {
           console.log(error.responseText);
          }
        });

Whereas your controller method will look like 而你的控制器方法看起来像

[HttpPost]
public JsonResult getLayerlist(string data)
{
    OpenlayerLayer ol = new OpenlayerLayer("Test: GParcel", "Parcels", "http://127.0.0.1:8080/geoserver/test/wms", "", true, "EPSG:900913", "5725524.327803587, 3835467.5859751734, 5729564.058979791, 3841404.792330884");

    return Json(ol , JsonRequestBehavior.AllowGet);//automatically converts to Json
}
function loadopenlayers(result){
    var newResult = JSON.stringify(result);
    //Now newResult is a json

}

You can call through ajax 你可以通过ajax打电话

 $.ajax({
    url: '/ControllerName/getLayerlist',
    type: "POST",
    cache:false, 
    success: function (data) {
        //You can get your result data here
        console.log("Data :"+JSON.stringify(data));
        loadopenlayers(data);
    },error: function (error) {
       console.log(error.responseText);
    }
});

function loadopenlayers(data){
    var newResult = JSON.stringify(data);//This is your JSON result
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM