【问题标题】:how to get selected text of select2 and use it in a sql statement如何获取select2的选定文本并在sql语句中使用它
【发布时间】:2015-08-25 13:05:24
【问题描述】:

我正在尝试获取 select2 元素的选定文本并在自定义 sql 语句中使用它,该语句将返回 json,我会将 json 呈现为 morris 条形图...谁能帮助我完成我需要做的事情?您将在下面找到我目前拥有的代码。提前致谢

达尔:

public DataTable GetBookingTrend()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("select WEEKNO, YEARNO, SUM(NOOFWEEKS) from BOOKINGTREND where ( SCHOOLCOUNTRY in (select2.text)) group by weekno, yearno");
            DataTable dt;

            using (SqlDataAdapter da = new SqlDataAdapter(strBuilder.ToString(), connStr))
        {
            dt = new DataTable();
            da.Fill(dt);
        }
            return dt;
        }

服务:

public List<BookingTrend> GetBookingTrendList()
    {
        List<BookingTrend> bookingtrends = new List<BookingTrend>();
        sqlDal dal = new sqlDal();

        foreach (DataRow item in dal.GetBookingTrend().Rows)
        {
            BookingTrend bookingtrend = new BookingTrend();
            bookingtrend.weekno = (int)item["WEEKNO"];
            bookingtrend.yearno = (int)item["YEARNO"];
            bookingtrends.Add(bookingtrend);
        }
        return bookingtrends;
    }

控制器:

public ActionResult BookingTrendRead()
    {
        Services.sqlService ss = new Services.sqlService();

        return Json(ss.GetBookingTrendList(), JsonRequestBehavior.AllowGet);
    }

HTML:

<div class="panel panel-info">
    <div class="panel-heading">
        Booking Trend
    </div>
<div class="panel-body">
    <div id="booking-trend-bar" style="height: 250px"></div>
</div>

HTML:

<div class="form-group col-lg-3 col-sm-6">
     <select class ="form-control" id="SchoolDestinations" style="width:385px;" multiple>
          <option></option>
     </select>
</div> 

文件准备好了:

$(document).ready(function () {

 function createChart(data) {
        var mychart = new Morris.Bar({
            element: 'booking-trend-bar',
            data: data,
            xkey: 'Country',
            ykeys: ['weekno','yearno'],
            labels: ['Week', 'Year']
        });
    }

    $.ajax({
        type: "GET",
        url: '@Url.Action("BookingTrendRead", "Main")',
        dataType: 'json',
        success: function (data) {
            var data = JSON.parse(data);

            createChart(data);
        },
        error: function () {
            alert("Error loading data! Please try again.");
        }
    });

$("#SchoolDestinations").select2({
        minimumInputLength: 0,
        placeholder: "School Destinations"
});

$.ajax({
    type: 'POST',
    url: '@Url.Action("SchoolRead", "Main")',
    dataType: 'json',
        success: function (data) {
            $.each(data, function (i, item) {
                $('<option value="' + item.Id + '">' + item.Text + '</option>').appendTo('#SchoolDestinations');
            });
        },
    error: function () {
        console.log('err')
    }
});
});

【问题讨论】:

  • 表单元素的值不会随表单一起发布吗?或者,如果您正在使用该 AJAX 请求,为什么不使用它发送任何数据?该服务器端代码在哪里使用?不清楚你在做什么。
  • 现在我所拥有的只是渲染 select2 元素的 ajax,我要做的是获取选定的值并将其绑定到 sql 语句..
  • 但是这些代码实际上是如何组合在一起的?是什么用 SQL 语句调用该代码?
  • 嗨大卫,我已经用整个逻辑更新了这个问题,很抱歉之前没有更好地解释。
  • 老实说,您的第一步是学习 MVC 的基础知识。我可以告诉你,你应该为你的select 元素添加一个name 属性,类似于name="schoolDestinations"。然后在操作方法中添加一个参数,例如:public ActionResult BookingTrendRead(string[] schoolDestinations) 但是,如果您不了解您正在使用的框架的基础知识,这些建议只会让您到目前为止。 (更不用说我什至不知道你的请求是不是这样设置的。)你正在使用 ASP.NET MVC,所以你的第一步是学习 ASP.NET MVC。

标签: c# sql asp.net-mvc-5 jquery-select2


【解决方案1】:

您需要从 sql 字符串中拆分出您选择的名称:

strBuilder.Append("select WEEKNO, YEARNO, SUM(NOOFWEEKS) from BOOKINGTREND where ( SCHOOLCOUNTRY in (" + select2.selectedText + ")) group by weekno, yearno");

【讨论】:

  • 那么select2.selectedText从何而来?
  • 好的,我回答时没有完整的代码,假设有一个具有该名称的服务器端控件
猜你喜欢
  • 2015-05-28
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多