我是新来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