我想傳遞一個值,用戶從下拉列表中選擇「國家」從視圖到控制器,我試圖通過HTTP POST方法檢索它,並我沒有成功。下面是我的看法代碼:傳遞值從下拉列表中選擇從視圖到控制器
@using WebApplication1.Models
@using WebApplication1.Controllers
@model Country
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<h2>Airport List</h2>
@Html.Label("Airports", "Airports")
<select name="Airports">
@foreach (var airport in ViewBag.EuropeanAirports)
{
<option value="@(airport.name)">@(airport.name)</option>
}
</select>
@Html.Label("Country", "Country")
@Html.DropDownListFor(c =>c.country, new SelectList(ViewBag.countries,
"country", "country"), "Select Country")
}
這裏是我的控制器:
public class AirportController : Controller
{
// GET: HelloWorld
public ActionResult Create()
{
IEnumerable<Airport> airports = GetAirports();
//LINQ QUERY TO RETRIEVE ALL EUROPEAN AIRPORTS
IEnumerable<Airport> EuropeanAirports = from n in airports
where n.continent.Equals("EU")
select n;
IEnumerable<Country> countries = GetCountries();
ViewBag.countries = countries;
ViewBag.EuropeanAirports = EuropeanAirports;
return View(new Country());
}
,這裏是我的國家模型:
public class Country
{
public string country { get; set; }
public string abbr { get; set; }
}
再次我的目標是檢索值這是用戶從國家下拉列表中選擇的。我不知道是否應該添加一個post方法來創建,我不知道如何將選定的值從視圖傳遞給控制器。