我正在从天气地图API获取城市的天气,我想通过使用复选框值来提取所选城市的天气数据。我无法将所选城市的值传递给api。
public ActionResult Index(string cities)
{
ClimateModel openWeatherMap = FillCity();
if (cities != null)
{
string apiKey = "e2c267b5316573acf93d5ee47a9d05e0";
HttpWebRequest apiRequest = WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?id=" + cities + "&appid=" + apiKey + "&units=metric") as HttpWebRequest;
string apiResponse = "";
using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
//var rootObject = JsonConvert.DeserializeObject<ResponseWeather>(apiResponse);
var rootObject = JsonConvert.DeserializeObject<ResponseWeather>(apiResponse);
StringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>Weather Description</th></tr>");
sb.Append("<tr><td>City:</td><td>" + rootObject.name + "</td></tr>");
sb.Append("<tr><td>Country:</td><td>" + rootObject.sys.country + "</td></tr>");
sb.Append("<tr><td>Country Sun Rise:</td><td>" + rootObject.sys.sunrise + "</td></tr>");
sb.Append("<tr><td>Country Sun Sete:</td><td>" + rootObject.sys.sunset + "</td></tr>");
//sb.Append("<tr><td>Wind:</td><td>" + rootObject.wind.speed + " Km/h</td></tr>");
sb.Append("<tr><td>Current Temperature:</td><td>" + rootObject.main.temp + " °C</td></tr>"
sb.Append("<tr><td>Weather:</td><td>" + rootObject.weather[0].description + "</td></tr>");
sb.Append("</table>");
openWeatherMap.apiResponce = sb.ToString();
}
else
{
if (Request.Form["submit"] != null)
{
TempData["SelectOption"] = -1;
}
}
return View(openWeatherMap);
}