使用javascript从mvc中的列表中获取特定列(get specific column from list in mvc using javascript)

编程入门 行业动态 更新时间:2024-10-23 09:31:44
使用javascript从mvc中的列表中获取特定列(get specific column from list in mvc using javascript)

我是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 }

更多推荐

本文发布于:2023-08-07 07:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表中   mvc   javascript   list   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!