【问题标题】:how to give default value for multiple dropdown(Country and City)如何为多个下拉列表(国家和城市)提供默认值
【发布时间】:2013-08-01 02:21:37
【问题描述】:

我尝试为城市和国家/地区制作下拉列表.. 选择下拉国家/地区时,下拉国家/地区显示国家/地区的城市..

这是我的代码

控制器

        public ActionResult Customer()
        {
            List<Country> country = _CustomerService.GetCountryForDropDown();
            CustomerViewModel model = CustomerBuilder.BuildCountries(country);
            return View(model);
        }

        [HttpPost]
        public ActionResult GetCityByCountry(int CountryID)
        {
            List<City> city = _CustomerService.GetCityForDropDown();
            CustomerViewModel model = CustomerBuilder.BuildCities(city);

            List<DDCity> getCityFromCountry = new List<DDCity>();
            getCityFromCountry = model.DDCityModel.Where(r => r.CountryID == CountryID).ToList();
            SelectList CityinCountry = new SelectList(getCityFromCountry, "CityID", "CityName", 0);
            return Json(CityinCountry);
        }

我的 viewModel 和 Function 从 Dropdown 提交值到 ViewModel

public class DDCountry
    {
        public int CountryID { get; set; }
        public string CountryName { get; set; }
    }

    public class DDCity
    {
        public int CountryID { get; set; }
        public int CityID { get; set; }
        public string CityName { get; set; }
    }
public class CustomerViewModel
    {
        public List<CustomerItem> ListCustomer { get; set; }
        public CustemerRequest Request { get; set; }
        public EditCustomer EditCustomer { get; set; }
        public List<DDCountry> DDCountryModel { get; set; }
        public List<DDCity> DDCityModel { get; set; }
        public SelectList GetCity { get; set; }

    }

 public static CustomerViewModel BuildCities (List<City> city)
        {
            CustomerViewModel model = new CustomerViewModel();
            model.DDCityModel = new List<DDCity>();

            foreach (var cities in city)
            {
                DDCity ct = new DDCity
                {
                    CityID = cities.CityID,
                    CityName = cities.CityName,
                    CountryID = cities.CountryID
                };
                model.DDCityModel.Add(ct);
            }
            return model;
        }

        public static CustomerViewModel BuildCountries(List<Country> country)
        {
            CustomerViewModel model = new CustomerViewModel();
            model.DDCountryModel = new List<DDCountry>();

            foreach (var countries in country)
            {
                DDCountry ct = new DDCountry
                {
                    CountryID = countries.CountryID,
                    CountryName = countries.CountryName
                };
                model.DDCountryModel.Add(ct);
            }
            return model;
        }

查看

<script type="text/javascript">
    function GetCity(_CountryID) {
        var procemessage = "<option value='0'> Please wait...</option>";
        $('#ddlCity').html(procemessage).show();
        var url = "/MasterDataCustomer/GetCityByCountry/";

        $.ajax({
            url: url,
            data: { CountryID: _CountryID },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select City</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $('#ddlCity').html(markup).show();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    }
</script>

@using (Html.BeginForm("", ""))
                {                    
                    @Html.Label("Country - City")
                    @Html.DropDownListFor(m => m.DDCountryModel, new SelectList(Model.DDCountryModel, "CountryID", "CountryName"), new { @id = "ddlstate", @style = "width:100px;margin-left:38px;", @onchange = "javascript:GetCity(this.value);" })
                    <select id="ddlCity" name="ddlCity" style="width: 100px; margin-left: 5px;"></select>
                }

我尝试使用会话从下拉列表中提供默认值,但是,它不起作用。 我从我的控制器中给出默认值,但是,就在我的下拉列表中。 如何从国家和城市给出默认值,在国家时,默认值为“意大利”,默认值为“罗马”,DropDown City 可以显示“意大利”中的所有城市

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

您可以在国家/地区拥有一个名为 defaultCityId 的字段。并且还可以拥有 City IsSelected 的属性。然后,当您构建城市列表时,只需将特定的默认城市 IsSelected 属性设置为 true。 然后在客户端,

for (var x = 0; x < data.length; x++) {
  markup += "<option selected="+data[x].IsSelected? "Selected":""+ " value=" + data[x].Value + ">" + data[x].Text + "</option>";
}

【讨论】:

    【解决方案2】:

    这样你就可以为你的dropdownlist设置default value

    型号

     public class CustomerModel
        {
    
        public int CustomerNameId { get; set; }
    
        public string customerIdName { get; set; }
    
        public List<SelectListItem> CustomerNameIdList { get; set; }
     }
    

    控制器

     model.CustomerNameIdList.Insert(0, (new CustomerModel { CustomerNameId = 0, customerIdName = " No Organization Size" }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2012-03-16
      • 2018-12-11
      相关资源
      最近更新 更多