我正在使用天气API,在该API中,用户从控制器作为参数接收的视图中搜索特定城市或国家/地区的天气,有关天气的信息已完全可用,但我无法将所有这些信息从控制器中返回。
查看搜索内容
<form action="searchbyname" method="post">
<input type="text" name="weather" placeholder="Find your location...">
<input type="submit" value="Find">
</form>
控制器
public ActionResult searchbyname(string weather)
{
string appId = "f40a39abac667183c127adefffcf88ed";
string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&APPID={1}", weather, appId);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
if (json.IndexOf("Error") == -1)
{
WeatherInfo weatherInfo = (new JavaScriptSerializer()).Deserialize<WeatherInfo>(json);
ViewBag.citycountry = weatherInfo.name + "," + weatherInfo.sys.country;
ViewBag.ImageUrl = string.Format("http://openweathermap.org/images/flags/{0}.png", weatherInfo.sys.country.ToLower());
ViewBag.des = weatherInfo.weather[0].description;
//weatherimage
ViewBag.ImageUrl = string.Format("http://openweathermap.org/img/w/{0}.png", weatherInfo.weather[0].icon);
ViewBag.temp = string.Format("{0}°С", Math.Round(weatherInfo.main.temp, 1));
}
}
return View();
}
查看应在其中显示数据
<table id="tblWeather" border="0" cellpadding="0" cellspacing="0" style="display:none">
<tr>
<th colspan="2">
Weather Information
</th>
</tr>
<tr>
<td rowspan="3">
<img id="imgWeatherIcon" />
</td>
</tr>
<tr>
<td>
<span id="citycountry">@ViewBag.citycountry</span>
<img id="imageurl" src="@ViewBag.ImageUrl" />
<span id="des">@ViewBag.des</span>
</td>
</tr>
模型类
public class ClimateModel
{
public class WeatherInfo
{
public Coord coord { get; set; }
public Sys sys { get; set; }
public List<Weather> weather { get; set; }
public Main main { get; set; }
public int dt { get; set; }
public string name { get; set; }
}
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Sys
{
public string country { get; set; }
}
public class Weather
{
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public int humidity { get; set; }
}
}
}
答案 0 :(得分:0)
我认为您在WebForms
和MVC
之间混用了一些概念。 runat="server"
上没有asp:Label
,asp:Image
或MVC
。您必须使用纯HTML元素。
您的代码应类似于:
<table id="tblWeather" border="0" cellpadding="0" cellspacing="0" style="display:none">
<tr>
<th colspan="2">
Weather Information
</th>
</tr>
<tr>
<td rowspan="3">
<img id="imgWeatherIcon" />
</td>
</tr>
<tr>
<td>
<span id="citycountry">@ViewBag.citycountry</span>
<img id="imageurl" src="@ViewBag.ImageUrl" />
<span id="des">@ViewBag.des</span>
</td>
</tr>
</table>
编辑1:正如@ erik-philips所指出的,您应该真正创建一个模型类。
编辑2:看看HTML Helpers。将模型绑定到视图时,可以使您的工作变得更轻松。
答案 1 :(得分:0)
正如Erik在评论中指出的那样,您可以使用模型来捕获api响应,然后将其放在视图包或视图数据中,以便能够在视图内部访问它。
ViewBag.WeatherInfo = weatherInfo;
然后在您的视图内可以执行以下操作:
var weatherInfo = ViewBag.WeatherInfo as WeatherInfo
这样,您就可以在视图内访问对象
请按照此处的要求在您的情况下使用它
@{
var weatherInfo = ViewBag.WeatherInfo as WeatherInfo
}
<table id="tblWeather" border="0" cellpadding="0" cellspacing="0" style="display:none">
<tr>
<th colspan="2">
Weather Information
</th>
</tr>
<tr>
<td rowspan="3">
<img id="imgWeatherIcon" />
</td>
</tr>
<tr>
<td>
<span id="citycountry">@weatherInfo.name , @weatherInfo.sys.country</span>
<img id="imageurl" src="your image link" />
<span id="des">@weatherInfo.weather.First().description</span>
</td>
</tr>