I generate a JSON file for a city which contains the Interesting Place information of that city. My JSON file look like this-
{
"Flensburg":[
{
"Name": "Flensburg Firth",
"Shorttext": "Flensburg Firth or Flensborg Fjord ....",
"Longitude": 9.42901993,
"Latitude": 54.7959404,
"Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Flensborg_Fjord_ved_bockholmwik.jpg/400px-Flensborg_Fjord_ved_bockholmwik.jpg"
},
{
"Name": "Naval Academy Mürwik",
"Shorttext": "The Naval Academy Mürwik is the main train....",
"Longitude": 9.45944444,
"Latitude": 54.815,
"Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/MSM-hauptgebaeude.jpg/400px-MSM-hauptgebaeude.jpg"
},
{
"Name": "Nordertor",
"Shorttext": "The Nordertor is an old town gate in Flensburg, Germany....",
"Longitude": 9.43004861,
"Latitude": 54.79541778,
"Images":"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG/266px-Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG"
}
]
}
Now in Model I created two classes POI.cs and RootObject.cs to get the object from this Json. These two classes look like this-
namespace Test2_search.Models
{
public class POI
{
public string Name { get; set; }
public string Shorttext { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public string Image { get; set; }
}
}
namespace Test2_search.Models
{
public class RootObject
{
public List<POI> poi { get; set; }
}
}
Now in the controller I at first implement a HttpPost to write the name in TextBox and then I created ActionResult GMap where I deserialize JSON data which I stored in APP_data.I want to get name in dynamic way.So that if I write in textbox Berlin, it will show all the deserialized json data for Berlin. if I write Flensburg it will show all the deserialized JSON data for Flensburg. The code I wrote for this method is-
namespace Test2_search.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(City objCityModel)
{
string name = objCityModel.Name;
return View();
}
public ActionResult GMap(City objCityModel)
{
string name = objCityModel.Name;
ViewBag.Title = name;
var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/"+name+".json"));
var json = JsonConvert.DeserializeObject<RootObject>(ReadJson);
ViewBag.Name= json.poi.First().Name;
ViewBag.ShortText = json.poi.First().Shorttext;
ViewBag.Latitude =json.poi.First().Latitude;
ViewBag.Longitude =json.poi.First().Longitude;
ViewBag.Image =json.poi.First().Image;
return View();
}
}
}
Now in Index.cshtml I implement a textbox to write the name of the city-
@model Test2_search.Models.City
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("GMap", "Home"))
{
<div class="wrapper wrapper-content">
<div class="row">
<div class="col-sm-12">
@Html.TextBoxFor(m => m.Name)
<label for="somevalue">City Name</label><input type="submit" id="City" name="City" value="Search" />
</div>
</div>
</div>
}
after writing the name in textbox it will go to Gmap.cshtml. In this page I want to show all the deserialized data from json file.
@model Test2_search.Models.City
@{
ViewBag.Title1 = "Google Map View";
}
<h2>@ViewBag.Title</h2>
<p>@ViewBag.ShortText</p>
<p>@ViewBag.Latitide</p>
<p>@ViewBag.ShortText</p>
But it is not working. it shows error -Source Error:
Line 45: var json = JsonConvert.DeserializeObject<RootObject> (ReadJson);
Line 46:
Line 47: ViewBag.Name= json.poi.First().Name;
Line 48: ViewBag.ShortText = json.poi.First().Shorttext;
Line 49: ViewBag.Latitude =json.poi.First().Latitude;