通过Ajax调用将数据从asp.net MVC控制器传递到View

时间:2017-11-30 22:00:07

标签: jquery ajax asp.net-mvc controller

大家好,我在使用ajax时遇到了一些问题,我试图了解发生了什么。好吧,我有这个控制器,它返回了Guider Model列表,这里是我的控制器:

[HttpGet]
        public JsonResult ShowGuiders(string latitude, string longitude)
        {
            double lat = DataTypeConvert.ConvertToDouble(latitude);
            double lon = DataTypeConvert.ConvertToDouble(longitude);
            GeoCoordinate geoCoordinateFrom = new GeoCoordinate(lat, lon);

            var nearbyGuiders = (from guider in db.Guiders.AsEnumerable()
                                 let distance = geoCoordinateFrom.GetDistanceTo(new GeoCoordinate(guider.Latitude.Value, guider.Longitude.Value))
                                 where distance <= 100000
                                 orderby distance ascending
                                 select guider).ToList();
            return Json(nearbyGuiders, JsonRequestBehavior.AllowGet);

        }

这是我的剧本:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnGet").click(function () {
                var coordinate = {
                    "Latitude": $("#latitude").val(),
                    "Longitude": $("#longitude").val()
                };
                $.ajax({
                    type: 'GET',
                    url: '@Url.Action("ShowGuiders", "Guider")',
                    data: { latitude: coordinate.Latitude, longitude: coordinate.Longitude },
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {
                        // Result
                        alert("Succes");
                    },
                    error: function (response) {
                        //do your own thing
                        alert("fail");
                    }
                });
            }); //end .submit()
        });
    </script>

最后这是我的导师模型:

public class Guider
    {
        [Key]
        [ForeignKey("ApplicationUser")]
        public string GuiderID { get; set; }
        //code...
        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
        public bool IsAccepted { get; set; }

        [ForeignKey("CancellationPolicy")]
        public int? CancellationPolicyID { get; set; }

        [ForeignKey("Premium")]
        public int? PremiumID { get; set; }

        public virtual Premium Premium { get; set; }
        public virtual CancellationPolicy CancellationPolicy { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }

        public virtual ICollection<UnavailableDate> UnavailableDates { get; set; }

    }

问题是,每当我试图返回Guide of Guider时,没有发生任何事情,并且ajax的错误功能事件正在提高。所以我试图只返回一个导向器的对象,并且evrything正在完美地工作。 这个控制器正在运行:

[HttpGet]
        public JsonResult ShowGuiders(string latitude, string longitude)
        {

            myGuider.Country = "Greece";
            return Json(myGuider, JsonRequestBehavior.AllowGet);

        }

所以我发现Json没有支持我的对象层次结构。所以我用anononymus对象解决了我的问题。我像这样修改了控制器,然后返回我需要的属性:

[HttpGet]
        public JsonResult ShowGuiders(string latitude, string longitude)
        {
            double lat = DataTypeConvert.ConvertToDouble(latitude);
            double lon = DataTypeConvert.ConvertToDouble(longitude);
            GeoCoordinate geoCoordinateFrom = new GeoCoordinate(lat, lon);

            var nearbyGuiders = (from guider in db.Guiders.AsEnumerable()
                                 let distance = geoCoordinateFrom.GetDistanceTo(new GeoCoordinate(guider.Latitude.Value, guider.Longitude.Value))
                                 where distance <= 100000
                                 orderby distance ascending
                                 select new { country = guider.Country } ).ToList();
            return Json(nearbyGuiders, JsonRequestBehavior.AllowGet);

        }

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$("#btnGet").click(function () 
{
    var coordinate = {
        "Latitude": $("#latitude").val(),
        "Longitude": $("#longitude").val()
    };
    $.ajax({
             type: 'GET',
             url: '@Url.Action("ShowGuiders", "Guider")',
             data: { 
                      latitude: coordinate.Latitude, 
                      longitude:coordinate.Longitude 
             },
             success: function (result) {
                        // Result
                        alert("Succes");
             },
             error: function (response) {
                        //do your own thing
                        alert("fail");
             }
   });
&#13;
&#13;
&#13;