从下拉列表中获取基于其他的价值

时间:2019-09-06 18:16:05

标签: c#

我需要根据下拉列表中的选定项从另一列获取值。

所以我有一个包含3个值的表:

  1. countryID
  2. countryDescription
  3. countryAbbreviature

,我有一个下拉列表,如下所示:

[jinja2: **/templates/**.jinja2]
extensions=jinja2.ext.i18n
encoding = utf-8

[compile_catalog]
directory = Genetic/locale
domain = Genetic
statistics = true

[extract_messages]
add_comments = TRANSLATORS:
output_file = Genetic/locale/Genetic.pot
width = 80
input_dirs = Genetic/templates

[init_catalog]
domain = Genetic
input_file = Genetic/locale/Genetic.pot
output_dir = Genetic/locale

[update_catalog]
domain = Genetic
input_file = Genetic/locale/Genetic.pot
output_dir = Genetic/locale
previous = true

我需要的是,当用户选择像阿富汗这样的缩写时,其缩写是AF,因此我需要将缩写存储在变量中,但是到目前为止,我在网站上发现的下拉列表始终只有2个值,就像我上面所做的那样。

2 个答案:

答案 0 :(得分:0)

没有直接的方法,但这是一种解决方法。

将缩写与ID设置为下拉列表的DataValueField:

public void fillCountry()
{
  //Fills the list with the countries in the database
  List<Country> countryList = countryService.getCountries();

  var dataSource = countryList.Select(c=> new { DataValueField = 
  c.countryID + "~" + c.countryAbbreviature, DataTextField = c.countryDesc }).ToList();

  ddlCountry.DataSource = dataSource;
  ddlCountry.DataTextField = "DataTextField";
  ddlCountry.DataValueField = "DataValueField";

  ddlCountry.DataBind();
}

在服务器端,可能会在按钮单击事件中按分隔符将其分隔,并获得ID和缩写:

protected void Button1_Click(object sender, EventArgs e)
{
  string selectedValue=ddlCountry.SelectedValue;
  int id = int.Parse(selectedValue.Split('~').First());
  string abreviation=selectedValue.Split('~').Last();
}

答案 1 :(得分:0)

假设您的表名为“国家/地区”:

var yourSelectedId = YourDropDownList.SelectedValue.ToString();

string yourVariable = dbContext.Countries.Where(x => x.countryID == yourSelectedId).FirstOrDefault().countryAbbreviature;