模型的IEnumerable我有一個模型叫Airport
,我試圖從控制器傳遞一個IEnumerable<Airport>
到視圖顯示,鑑於
public ActionResult Index()
{
IEnumerable<Airport> airports = GetAirports("https://raw.githubusercontent.com/jbrooksuk/JSON-Airports/master/airports.json");
return View(airports);
}
我的目標是在我看來,一個下拉列表顯示機場名稱列表。我無法從控制器變量機場轉移到我的觀點,我不斷收到錯誤:
The name 'airports' does not exist in the current context
這裏是我的觀點
@using WebApplication1.Models
@using WebApplication1.Controllers
@model WebApplication1.Models.AirportList
@{
ViewBag.Title = "Index";
}
<select name="Airports">
@foreach (var link in airports)
{
<option value="@(link)">@(link)</option>
}
</select>
這裏使用的代碼是模型Airport
我使用
public class Airport
{
public string iata { get; set; }
public decimal lon { get; set; }
public string iso { get; set; }
public int status { get; set; }
public string name { get; set; }
public string continent { get; set; }
public string type { get; set; }
public decimal lat { get; set; }
public string size { get; set; }
}
你檢查過Json響應嗎? –