我是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 ";到达Dataset 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" })现在,我需要在按钮点击的javascript中获取下拉列表'lstA'的'Area'列的值
在javascript中
function AB(){ $('#lstVenue :selected').val(); // this gives selected value }如何获得列'Area'的值?
I am new to MVC and trying to build put a simple web application.
I have a Class - ListItems
public class ListItems { public string Display { get; set; } public string Value { get; set; } public string Area { get; set; } public string LongDescription { get; set; } }From controller -
I am using function to populate values of dropdownlist .Data is populated to ListItems
List<ListItems> Lst= new List<ListItems>(); string query = @"SELECT DISTINCT A.[COLA],A.[COLB],[COLC] FROM tableA ";getting to Dataset 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); }In View
@Html.DropDownListFor(model => model.COLA, new SelectList(Model.VenueList, "Value", "Display","Area"), "- Please select a Value-", new { id = "lstA" })Now, I need to get value of 'Area' Column of dropdownlist 'lstA' in javascript on button click
In javascript
function AB(){ $('#lstVenue :selected').val(); // this gives selected value }How to get value of column 'Area' ?
最满意答案
由于下拉列表不能绑定3个值,您必须将Value和Area设置为具有唯一字符的选项值(我使用'#'作为示例),如下所示:
// 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" })然后,您可以使用JavaScript(jQuery)获取“Value”和“Area”,如下所示:
var selected = $('#lstA :selected').val(); var splited = selected.split("#"); var value = splited[0]; var area = splited[1]; if (area == "0"){ // Do something }Since a dropdown list cannot bind 3 values, you have to set Value and Area to values of options with a unique character (I used '#' as an example) like this:
// 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" })Then, you can get both "Value" and "Area" with JavaScript(jQuery) like this:
var selected = $('#lstA :selected').val(); var splited = selected.split("#"); var value = splited[0]; var area = splited[1]; if (area == "0"){ // Do something }更多推荐
发布评论