嘗試刪除這些RUNAT服務器標籤。他們不應該在ASP.NET MVC中使用。您的選擇也沒有名稱。如果輸入元素沒有名稱,它將不會提交任何內容。此外,您的選項標籤必須有這表明什麼,如果選擇該選項的值將被髮送到服務器的值屬性:
<form action="/Home/Create" method="post">
<p>
<select id="BLAHBLAH2" name="BLAHBLAH2">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="Change Year" />
</p>
</form>
但在ASP.NET MVC產生形式正確的方法是使用HTML傭工。根據您使用的視圖引擎,語法可能會有所不同。下面是與Razor視圖引擎爲例:
@model MyViewModel
@using (Html.BeginForm("Create", "Home"))
{
<p>
@Html.DropDownListFor(x => x.SelectedYear, Model.Years)
<input type="submit" value="Change Year" />
</p>
}
在這裏,您有一個強類型以某些給定視圖模型:
public class MyViewModel
{
public string SelectedYear { get; set; }
public IEnumerable<SelectListItem> Years
{
get
{
return Enumerable
.Range(2010, 4)
.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = x.ToString()
});
}
}
}
這是由一些控制器動作人口將呈現這樣的觀點:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Create(MyViewModel model)
{
... model.SelectedYear will contain the selected year
}
}
是不是'runat =「server」'一個webforms的東西? –