我是新來asp.net的MVC ...... &需要幫助我下面的問題:填充html.textbox在asp.net mvc的
加載窗體時我的國家下拉列表有一些值。我希望當用戶從下拉列表中選擇一個值時,它應該返回到控制器,並調用數據庫來根據所選國家回顧CountryCode值。我如何模擬回傳呼叫?
由於提前
Deepthi
我是新來asp.net的MVC ...... &需要幫助我下面的問題:填充html.textbox在asp.net mvc的
加載窗體時我的國家下拉列表有一些值。我希望當用戶從下拉列表中選擇一個值時,它應該返回到控制器,並調用數據庫來根據所選國家回顧CountryCode值。我如何模擬回傳呼叫?
由於提前
Deepthi
你需要在你的ASP下拉菜單設置的AutoPostBack = 「真」。另外你的控制需要設置你的OnSelectedIndexChanged =「SomeFunction」。這將在您的下拉列表中的索引更改時調用代碼隱藏中的函數。這裏有一個例子
<asp:DropDownList ID="dropDownID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnDropdownIndex_Change"> </asp:DropDownList>
相反,經典的WebForms在ASP.NET MVC中有沒有這樣的概念作爲回傳。於是開始與你需要的是要表現你的數據模型:
public class MyViewModel
{
public string SelectedCountry { get; set; }
public IEnumerable<SelectListItem> Countries { get; set; }
}
然後,你將需要一個控制器,它定義了兩個動作:呈現形式之一,另一個處理提交:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// you probably would fetch those from the database
Countries = new SelectList(new[]
{
new { Value = "FR", Text = "France" },
new { Value = "US", Text = "USA" }
}, "Value", "Text")
};
return View(model);
}
[HttpPost]
public ActionResult Index(string selectedCountry)
{
// selectedCountry will contain the code that you could use to
// query your database
return RedirectToAction("index");
}
}
最後,你可能會拋出一個強類型的視圖模型將包含標記:
<% using (Html.BeginForm()) { %>
<%: Html.DropDownListFor(x => x.SelectedCountry, Model.Countries) %>
<input type="submit" name="OK" />
<% } %>
如果沒有這個任何意義,你,我建議你reading the getting started tutorials。
問題是關於ASP.NET MVC而不是Web窗體。 – 2010-07-16 18:12:19