如何使用asp.net mvc中的多个下拉列表? [英] How to work with multiple dropdown lists in asp.net mvc?
问题描述
1:创建你的模型。
public < span class =code-keyword> class 员工
{
[ScaffoldColumn( false )]
public int EmployeeId { get ; set ; }
public string SelectedStateId {获得跨度>; set ; }
public 列表< state>国家{获取; set ; }
public string SelectedCityId {获得跨度>; set ; }
public 列表< city>城市{获取; set ; }
}
public class 状态{
public string ID { get ; set ; }
public string 名称{ get 跨度>; set ; }
}
public class 城市
{
public string ID { get ; set ; }
public string 名称{ get 跨度>; set ; }
public string StateID { get 跨度>; set ; }
}
< / city > < / 州 >
第2步:创建cshtml页面。
从控制器的索引操作方法创建视图。右键单击=>添加视图。
@ model YourNamespace.Models.Employee
< div class = formElements >
@ Html.Label(选择State,new {@class =control-label col-md-2})
@ Html.DropDownListFor(model => model.SelectedStateId,new SelectList(Model.States,ID,Name ,Null),new {@id =SelectedState})
< / div >
< div class = < span class =code-keyword> formElements style = display:none id = select-city-div >
< / div > ;
第3步:将以下Javascript添加到您创建的视图中第2步。
这里我们从下拉列表中获取所选状态的值,并将值传递给名为GetCities的方法。
< script type = text / javascript> ;
(' #SelectedState ')。change( function (){
var selectedStateValue =
(' #SelectedState选项:选中') .VAL();
// 获取所选州的值
if (selectedStateValue == NULL){
Hai i have two dropdownlists,in that my first dropdownlist is Select Country and second dropdownlist is select state ,Country information and state information ineed to bind from database,when i select country from first dropdown list and i need to display corresponding states in second dropdown list in asp.net mvc.any one help me please..Thanks in advance..
Step 1 : Create your Model.
public class Employee { [ScaffoldColumn(false)] public int EmployeeId { get; set; } public string SelectedStateId { get; set; } public List<state> States { get; set; } public string SelectedCityId { get; set; } public List<city> Citys { get; set; } } public class State { public string ID { get; set; } public string Name { get; set; } } public class City { public string ID { get; set; } public string Name { get; set; } public string StateID { get; set; } } </city></state>
Step 2 : Create your cshtml page.
Create a view from your index action method your controller. Right click => add View.
@model YourNamespace.Models.Employee <div class="formElements"> @Html.Label("Select State", new { @class = "control-label col-md-2" }) @Html.DropDownListFor(model => model.SelectedStateId, new SelectList(Model.States, "ID", "Name", "Null"), new { @id = "SelectedState" }) </div> <div class="formElements" style="display:none" id="select-city-div"> </div>
Step 3 : Add the below Javascript into your view created at step 2.
Here we are getting the value of selected state from dropdown and pass value to a method called "GetCities".
<script type="text/javascript">
('#SelectedState').change(function () { var selectedStateValue =
('#SelectedState option:selected').val(); // Get the values of selected state if (selectedStateValue == "NULL") {
这篇关于如何使用asp.net mvc中的多个下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!