javascript - 如何在 ASP.NET MVC 中的存储库上执行函数?

标签 javascript c# asp.net-mvc dropdown

我遇到了与此类似的问题,但该解决方案似乎不适用于此处。我正在尝试调用重载的存储库函数来根据 View 下拉列表中选择的选择选项对 View 数据进行排序。

我使用“onchange”事件来执行我的javascript,但我在尝试执行存储库函数时遇到了困难,因为重载不匹配,并且我不确定如何正确绑定(bind)它们。任何提供的帮助将不胜感激。

我的 Html View

<select id="sortSelect" onchange="CallChangeFunction()">
    <option value="default">Default</option>
    <option value="make">Make</option>
    <option value="model">Model</option>
    <option value="year">Year</option>
</select>

<script>
    function CallChangeFunction(val) {
        alert("Do something here to make it sort!");
    }
</script>

我的列表存储库

IEnumerable<ListingStat> GetListingStats(int inCompanyID, int inMonth, int inYear);


public IEnumerable<ListingStat> GetListingStats(int inCompanyID, int inMonth, int inYear)
{
    //var x = document.getElementById("sortSelect").value;

    // there's probably a way to make ef do this query in an efficient manner.. but, there aren't nice keys between listingstats and makemodel stats, and I'm not going to create them for the sake of ef
    var theSQL = "select d.DatePK, ls.*, " +
                 "mm.Make as MM_Make, mm.Model as MM_Model, mm.ListingsThatWereActive as MM_ListingCount, mm.MedianPriceExcludeZero as MM_MedianPrice, mm.PopularityRank as MM_PopularityRank, " +
                 "mm.PopularityMaxRank as MM_PopularityMaxRank, mm.AverageFavorites as MM_Favorites, mm.AverageSearchImpressions as MM_SearchImpressions, mm.AverageBuyerInquiries as MM_BuyerInquiries, mm.AveragePhoneInquiries as MM_PhoneInquiries, mm.AverageListingViews as MM_ListingViews, " +
                 "ymm.SupplyDemandIntegerPercentile as YMM_SupplyDemandPercentile, ymm.TotalListingViews as YMM_TotalListingViews " +
                 "from " +
                 "PerformanceDataMart.Dates d " +
                 "left outer join PerformanceDataMart.ListingStats ls on ls.DateFK = d.DatePK and ls.CompanyId = :CompanyID " +
                 "left outer join PerformanceDataMart.MakeModelStats mm on mm.DateFK = d.DatePK and mm.Make = ls.Make and mm.Model = ls.Model and mm.Year is null " +
                 "left outer join PerformanceDataMart.MakeModelStats ymm on ymm.DateFK = d.DatePK and ymm.Make = ls.Make and ymm.Model = ls.Model and ymm.Year = ls.Year " +
                 "where d.Month = :Month and d.Year = :Year";

    var theDB = new CCDB();

    List<ListingStat> theList = new List<ListingStat>();

    using (IDataReader aDR = theDB.OpenDataReader(theSQL, inCompanyID, inMonth, inYear))
    {
        while(aDR.Read())
        {
            ListingStat theListingStat = new ListingStat();
            theList.Add(theListingStat);

            theListingStat.ListingId = As.Integer(aDR["ListingId"]);
            theListingStat.Year = As.Integer(aDR["Year"]);
            theListingStat.Make = As.String(aDR["MM_Make"]);
            if (theListingStat.Make == "")          // shouldn't happen, but just in case
                theListingStat.Make = As.String(aDR["Make"]);

            theListingStat.Model = As.String(aDR["MM_Model"]);
            if (theListingStat.Model == "")             // shouldn't happen, but just in case
                theListingStat.Model = As.String(aDR["Model"]);
        }
    }

    return theList;
}

最佳答案

在页面上包含 Jquery 并尝试这个:

<script>
    $(function () {
        $("#sortSelect").change(function () {
            alert("test");
        });
    });
</script>

关于javascript - 如何在 ASP.NET MVC 中的存储库上执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45044441/

相关文章:

javascript - AngularJS - 在预先输入自动完成后填充其他字段

C# 为什么不能在 winform 构造函数中设置 GUI 特定操作?

c# - 如何创建具有动态对象类型的列表

c# - 列表框触摸滚动性能

asp.net-mvc - Razor 多行内联模板

asp.net - 在 ASP.NET MVC 中使用 <%$ %>

c# - 来自 C# 代码的 Javascript 仅在 Google Chrome 中刷新

javascript - 单击时更改链接的 href 属性

asp.net-mvc - 将 HttpContext.Current.User.Identity 传递给 WCF

javascript - angular 2服务注入(inject)问题