2017-12-29 42 views
0

私は、インデックス(プロジェクトのホームページ)から自分のプロジェクトのページに移動しようとしています。 これは1回だけ機能します。ページはmvcで一度だけ表示されます

このコードは、それが完璧に動作して

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") 

だから、インデックスページからであるparkingLotscontrollerからですビューimmediatly、 が来るが、クリエイティブ側にはしばらく時間がかかりますロードしてから がエラーをスローします。 問題は何ですか?問題を解決するにはどうすればよいですか? ありがとう!

+0

「JsonResult」を返す別のコントローラアクションを使用する代わりに、なぜ好奇心から、JSONをファイルに書き込んでいますか?これは複数のユーザーにとって問題になります。また、 'db.SaveChanges()'とは何ですか? –

+0

db.SaveChanges()は、dbを更新したままにしておきます.Jsonについては、このエリアでは新しいので、新しいContorllerを作成すると問題は解決するはずですか? – InbalK

答えて

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()を呼び出している理由はわかりません。

+0

最初に地図を表示するのに役立ちましたが、地図上にJsonの結果が表示されません。 TotalPDataアクション(Jsonを返すアクション)のリクエスト – InbalK

関連する問題