页面仅在mvc上指示一次

时间:2017-12-29 21:20:26

标签: asp.net-mvc google-maps-api-3 client-server

我试图从索引(项目主页)指向我项目的页面。 它只工作一次。

此代码来自parkingLotscontroller

 public ActionResult TotalPs()
    {
        ViewBag.Message = "TotalPs";
        var totalQuery =
              (from lot in db.parkingLots
               orderby lot.PricePerHour
               select new
               {
                   ID = lot.parkingLotID,
                   address = lot.addressParkingLot,
                   latitude = lot.latitudeParkingLot,
                   longtitude = lot.longtitudeParkingLot,
                   Status = lot.statusParkingLot,
                   PricePerHour = lot.PricePerHour
               })
             .Union(from pub in db.publicParkings
                    orderby pub.PricePerHourpublicParking
                    select new
                    {
                        ID = pub.publicParkingID,
                        address = pub.addressPublicParking,
                        latitude = pub.latitude,
                        longtitude = pub.longtitude,
                        Status = pub.statusParking,
                        PricePerHour = pub.PricePerHourpublicParking
                    });

        var data2 = totalQuery.ToList();

        var jsonString2 = JsonConvert.SerializeObject(data2);

        if (jsonString2 != null)
        {
            if (!Directory.Exists(Server.MapPath("~/Content/")))
            {
                Directory.CreateDirectory(Server.MapPath("~/Content/"));
            }

        }
        System.IO.File.WriteAllText(Server.MapPath("~/Content/TotalJson.json"), jsonString2);
        db.SaveChanges();
        return View();
    }

此代码来自视图

@{
    ViewBag.Title = "TotalPs";
}

<h2>TotalPs</h2>


<head>
    <style>
        #map {
            height: 700px;
            width: 1000px;
            border: 1px solid black;
            margin: 0 auto;
        }
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyApsEFrg9i2dlhq493ME30ETlDGDNbQvWI" type="text/javascript"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<br />
<br />

<div class="topnavI" align="center">
    <p style="font-size:16px;"> Enter the address to search for available parking spaces</p>
    <input type="text" placeholder="Search for address" size="40">
</div>
<br />
<br />


<div id="map"></div>
<script>
    var map;
    function initialize() {
        var mapProp = {
            center: new google.maps.LatLng(32.04772750000001, 34.7609645),
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map'), mapProp);
    };

    $(document).ready(function () {
        var url = "../../Content/TotalJson.json";
        initialize();
        $.getJSON(url, function (data) {
            $.each(data, function (i, field) {
                $('#list').append("<li>" + data[i].latitude + " & " + data[i].longtitude + "</li>");
                createMarker(data);

                function createMarker(data) {

                    var marker = new google.maps.Marker({
                        icon: 'https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png',
                        position: new google.maps.LatLng(data[i].latitude, data[i].longtitude),
                        map: map,
                        title: field.crossroad
                    });
                };
            });
        });

    });
</script>
<body>
</body>

索引页面

     @Html.ActionLink("INBAL", "TotalPs", "parkingLots")

所以在服务器端它完美运行,视图立即出现, 但是在cliet方面需要一段时间来加载然后 抛出错误。 有什么问题,我该如何解决? 谢谢!

1 个答案:

答案 0 :(得分:0)

只需执行一个返回JSON的控制器操作,然后从客户端调用它,而不是写入文件。如果多个用户访问您的应用程序,多个进程将尝试写入同一文件并出错。

public ActionResult TotalPs()
{
    ViewBag.Message = "TotalPs";
    return View();
}

public JsonResult TotalPData()
{
    var totalQuery =
          (from lot in db.parkingLots
           orderby lot.PricePerHour
           select new
           {
               ID = lot.parkingLotID,
               address = lot.addressParkingLot,
               latitude = lot.latitudeParkingLot,
               longtitude = lot.longtitudeParkingLot,
               Status = lot.statusParkingLot,
               PricePerHour = lot.PricePerHour
           })
         .Union(from pub in db.publicParkings
                orderby pub.PricePerHourpublicParking
                select new
                {
                    ID = pub.publicParkingID,
                    address = pub.addressPublicParking,
                    latitude = pub.latitude,
                    longtitude = pub.longtitude,
                    Status = pub.statusParking,
                    PricePerHour = pub.PricePerHourpublicParking
                });

    return Json(totalQuery);   
}

然后

$(document).ready(function () {
    var url = "@Url.Action("TotalPData", "ParkingLots")";

我不知道您为什么要调用db.SaveChanges(),因为您只是在阅读数据而不是更新任何内容。