如何在Asp.Net MVC 4中绑定Drpdownlist [英] How to Bind Drpdownlist's in Asp.Net MVC 4
本文介绍了如何在Asp.Net MVC 4中绑定Drpdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友
我想在asp.net MVC中绑定下拉列表
我有2个下拉列表1是国家1是状态我想要使用我想绑定的实体框架工作绑定这2个下拉列表。
选择国家后我想绑定状态名称下拉列表请帮我在Asp.net MVC4上如何做到这一点..
谢谢:
Ajay
Hi Friends
I want to bind dropdownlist in asp.net MVC
I have 2 dropdowns one is country 1 is State I want to Bind this 2 dropdowns using Entity Frame work i want to bind.
After Selecting Country i want to bind state Names Dropdownlist please help me how to do this on Asp.net MVC4..
Thanks :
Ajay
推荐答案
请检查相同的网址
http://www.a2zmenu.com/Blogs/MVC/How-to-fill-DropdownList-control-and-SelectedIndexChanged-event-in-MVC.aspx
Please check on same url
http://www.a2zmenu.com/Blogs/MVC/How-to-fill-DropdownList-control-and-SelectedIndexChanged-event-in-MVC.aspx
@{
ViewBag.Title = "Main";
}
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function GetState() {
var selectedValue =
('#ddlCountry')。val();
window.location.href ='/ Bind / GetStatesByID /?countryCode ='+ selectedValue;
}
< / script>
< h2> Main< / h2>
@using(Html.BeginForm())
{
@ Html.DropDownList(Country,(SelectList)ViewBag.countryList, - 选择你的国家 - ,new {id =ddlCountry, onchange =GetState();})
@ Html.DropDownList(State,(SelectList)ViewBag.stateList, - 选择你的州 - ,新{id =ddlState})
}
('#ddlCountry').val(); window.location.href = '/Bind/GetStatesByID/?countryCode=' + selectedValue; } </script> <h2>Main</h2> @using (Html.BeginForm()) { @Html.DropDownList("Country", (SelectList)ViewBag.countryList, "--Choose Your Country--", new { id = "ddlCountry",onchange="GetState();" }) @Html.DropDownList("State", (SelectList)ViewBag.stateList, "--Choose Your State--", new { id = "ddlState" }) }
public class BindController : Controller
{
//
// GET: /Bind/
public ActionResult Index()
{
ViewBag.countryList = GetAllCountrys();
ViewBag.stateList = GetAllStates();
return View("Main");
}
public ActionResult GetStatesByID(int? countryCode)
{
ViewBag.stateList = GetAllStatsByID(countryCode);
ViewBag.countryList = new SelectList(GetAllCountrys(), "Value", "Text", countryCode);
return View("Main");
}
private SelectList GetAllStatsByID(int? countryCode)
{
Data.TestEntities entity = new Data.TestEntities();
{
IEnumerable<SelectListItem> selectedStates = entity.States.Where(s => s.CountryID == countryCode).AsEnumerable().Select(m => new SelectListItem()
{
Text = m.StateName
,
Value = m.StateID.ToString()
});
return new SelectList(selectedStates, "Value", "Text", "--Select State--");
}
}
private SelectList GetAllCountrys()
{
Data.TestEntities entity = new Data.TestEntities();
{
IEnumerable<SelectListItem> country = entity.Countries.AsEnumerable().Select(m => new SelectListItem()
{
Text = m.CountryName
,
Value = m.CountryID.ToString()
});
return new SelectList(country, "Value", "Text","--Select Country--");
}
}
private SelectList GetAllStates()
{
Data.TestEntities entity = new Data.TestEntities();
{
IEnumerable<SelectListItem> states = entity.States.AsEnumerable().Select(m => new SelectListItem()
{
Text = m.StateName
,
Value = m.StateID.ToString()
});
return new SelectList(states, "Value", "Text", "--Select States--");
}
}
}
这篇关于如何在Asp.Net MVC 4中绑定Drpdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文