页面仅在mvc上指示一次

问题描述 投票:0回答:1

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

此代码来自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")

所以在服务器端它运行良好,视图立即出现,但在客户端,它需要一段时间加载,然后抛出一个错误。有什么问题,我该如何解决?谢谢!

asp.net-mvc google-maps-api-3 client-server
1个回答
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(),因为你只是在阅读数据而不是更新任何东西。

© www.soinside.com 2019 - 2024. All rights reserved.