如何使用asp.net mvc中的多个下拉列表? [英] How to work with multiple dropdown lists in asp.net mvc?

查看:67
本文介绍了如何使用asp.net mvc中的多个下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表,因为我的第一个下拉列表是选择国家,第二个下拉列表是选择状态,国家信息和状态信息无法从数据库绑定,当我从第一个下拉列表中选择国家,我需要显示相应的状态在asp.net mvc.any中的第二个下拉列表中,请帮助我..提前谢谢..

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆