ASP.NET MVC4 WebService应用-天气预报
Controller:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using ChildrenInterestClass.Weather; 7 namespace ChildrenInterestClass.Controllers 8 { 9 public class WeatherController : Controller 10 { 11 // 12 // GET: /Weather/ 13 14 public ActionResult Weather() 15 { 16 return View(); 17 } 18 19 [HttpPost] 20 public ActionResult Weather(FormCollection fc) 21 { 22 string cityName = fc["txtCityName"]; 23 WeatherWebService w = new WeatherWebService(); 24 string[] wes=new string[]{}; 25 wes=w.getWeatherbyCityName(cityName); 26 ViewData.Model=wes; 27 return View(); 28 } 29 } 30 }
View:
1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>天气查询</title> 11 <style> 12 div div { 13 clear: both; 14 width: 80px; 15 height: 35px; 16 line-height: 35px; 17 color: black; 18 font-family: 'Microsoft YaHei'; 19 text-align: left; 20 float: left; 21 } 22 23 .divContainer { 24 width: 600px; 25 height: auto; 26 line-height: 35px; 27 } 28 label { 29 color: #b200ff; 30 } 31 .div-big { 32 margin: 0 auto; 33 width: 900px; 34 margin-top: 50px; 35 } 36 </style> 37 </head> 38 <body> 39 <div class="div-big"> 40 @using (Html.BeginForm("Weather", "Weather", FormMethod.Post)) 41 { 42 <span>城市名(支持地级市、直辖市,暂不支持县级市,区等):</span> <input type="text" name="txtCityName" value=" " /> 43 <input type="submit" name="name" value="确定" /><br /> 44 if (ViewData.Model != null) 45 { 46 <div class="divContainer"> 47 <div>城市名:</div> 48 <label>@ViewData.Model[0]</label><span>--</span><label>@ViewData.Model[1]</label> 49 </div> 50 <div class="divContainer"> 51 <div>今日天气:</div> 52 <label>@ViewData.Model[6]</label> 53 <label>@ViewData.Model[5]</label> 54 <label>@ViewData.Model[7]</label> 55 </div> 56 <div class="divContainer"> 57 <div>详情:</div> 58 <label>@ViewData.Model[10]</label> 59 </div> 60 <div class="divContainer"> 61 <div>建议:</div> 62 <label>@ViewData.Model[11]</label> 63 </div> 64 <div class="divContainer"> 65 <div>查询时间:</div> 66 <label>@ViewData.Model[4]</label> 67 </div> 68 69 <div class="divContainer"> 70 <div>明日预报:</div> 71 <label>@ViewData.Model[13]</label> 72 <label>@ViewData.Model[12]</label> 73 <label>@ViewData.Model[14]</label> 74 </div> 75 } 76 } 77 </div> 78 </body> 79 </html>