【问题标题】:How to return multiple viewbag data to view from controller?如何从控制器返回多个视图包数据以查看?
【发布时间】:2019-05-12 17:32:24
【问题描述】:

我正在使用天气 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; }
        }
    }
}

【问题讨论】:

  • 为什么不像Model-View-Controller那样创建一个Model,而不是使用viewbag(从技术上讲它不是一个模型)。
  • @ErikPhilips 实际上我有模型类,也想使用它,但无法理解如何在控制器内部使用,因为模型类(天气信息)已经用于从 API 获取数据。你可以看到使用了 weatherinfo 对象。请告诉我如何使用。我编辑了我的问题,添加了模型类。

标签: c# asp.net-mvc razor openweathermap


【解决方案1】:

正如 Erik 在他的评论中指出的那样,您可以使用模型来捕获 api 响应,然后将其放入 viewbag 或 viewdata 以便能够在视图中访问它。

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>

【讨论】:

  • adam 是的,我已经使用模型类来捕获 api 响应。请写更多代码,以便我可以轻松理解。我如何在视图中使用
  • 你能告诉我如何在视图中使用 var weatherInfo = ViewBag.WeatherInfo 作为 WeatherInfo 吗?
  • 不幸的是,“as weatherinfo”在视图中显示错误我必须在控制器中进行一些更改,但我无法做到。
  • @AdilYar ViewBag 是动态的,这意味着你可以做 WeatherInfo weatherInfo = viewbag.WeatherInfo;你得到什么类型的错误。在视图中声明 WeatherInfo 命名空间时,也不要忘记完全限定它
【解决方案2】:

我认为您在 WebFormsMVC 之间混合了一些概念。 MVC 上没有 runat="server"asp:Labelasp:Image。您必须使用纯 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。将模型绑定到视图时,可以让您的生活更轻松。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多