我是MVC的新手并尝试构建一个简单的Web应用程序。
我有一个类 - ListItems
public class ListItems
{
public string Display { get; set; }
public string Value { get; set; }
public string Area { get; set; }
public string LongDescription { get; set; }
}
来自控制器 -
我正在使用函数来填充dropdownlist的值.Data填充到ListItems
List<ListItems> Lst= new List<ListItems>();
string query = @"SELECT DISTINCT A.[COLA],A.[COLB],[COLC] FROM tableA ";
转到数据集ds
ListItems item;
foreach (DataRow row in ds.Tables[0].Rows)
{
item = new ListItems();
item.Value = DBBase.ConvertToString(row["COLA"], string.Empty);
item.Display = DBBase.ConvertToString(row["COLB"], string.Empty);
item.Area = DBBase.ConvertToString(row["COLC"], string.Empty);
statusList.Add(item);
}
在视图中
@Html.DropDownListFor(model => model.COLA, new SelectList(Model.VenueList, "Value", "Display","Area"), "- Please select a Value-", new { id = "lstA" })
现在,我需要获得&#39; Area&#39;下拉列表列表&#39; lstA&#39;在javascript中 点击按钮
在javascript中
function AB(){
$('#lstVenue :selected').val(); // this gives selected value
}
如何获得列&#39;区域&#39;的价值?
答案 0 :(得分:0)
由于下拉列表无法绑定3个值,因此您必须将Value
和Area
设置为具有唯一字符的选项值(我使用&#39;#&#39;作为示例)像这样:
// Make sure '#' doesn't appear on "Value" nor "Area"
@Html.DropDownListFor(model => model.COLA, new SelectList(Model.VenueList.Select(v => new { Display = v.Display, Value = string.Concat(v.Value, "#", v.Area) }), "Value", "Display"), "- Please select a Value-", new { id = "lstA" })
然后,你可以同时获得&#34;价值&#34;和&#34; Area&#34;使用JavaScript(jQuery):
var selected = $('#lstA :selected').val();
var splited = selected.split("#");
var value = splited[0];
var area = splited[1];
if (area == "0"){
// Do something
}