Search code examples
javascriptphpjqueryasp.netgetjson

Read JSON file - asp.net (where to put - controller / view)


I am building a project is asp.net , and Im reading 2 JSON files that I create from a queries.

The loading of the json file is not working. He shows me an error while he trying to read it.

I think its because the controller and the location of the queries.

This is the controller:

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

    public JsonResult allParking()
    {
        var totalQueryParkingLot =
              from lot in db.parkingLots
              orderby lot.PricePerHour
              select lot;

        return Json(totalQueryParkingLot);
    }

And this is the part of the view:

    @{
      ViewBag.Title = "TotalPs";
     }

    $(document).ready(function () {
    var url2 = "@Url.Action("allParking","parkingLots")";
    initialize();
    $.getJSON(url2, function (data2) {
        $.each(data2, function (i, field) {
            createMarker(data2);

UPDATE http://www.siz.co.il/my.php?i=mrwmygmntmjz.png

http://www.siz.co.il/my.php?i=ynyzmltjiafg.png

http://www.siz.co.il/my.php?i=nzyyt42k3jit.png

Please Help Me


Solution

  • That "view" you are referring to should be in the Scripts folder.

    and then in the view that you want the scripts to render, you do:

    in the RegisterBundles of the BundleConfig.cs, you add the scripts into a bundle:

    bundles.Add(new ScriptBundle("~/bundles/bundledScripts").Include(
                "~/Scripts/scriptOne.js"));
    
    @Scripts.Render("~/bundles/bundledScripts")
    

    Update:

    1. the BundleConfig.cs can be found in the App_Start folder (see 1st pic)
    2. add the following code:

    bundles.Add(new ScriptBundle("~/bundles/bundledScripts").Include( "~/Scripts/scriptOne.js"));

    1. in the page where you want the scripts to take into effect, put this code:

      @Scripts.Render("~/bundles/bundledScripts")

    Watch out for the names in bundle and render. They have to match. Hope it helps you.