需要编写一个Jquery来获取所有的Selectlist选项值,将它们存储在一个数组中,并将它们发送给Action Me

Need to write a Jquery that gets all Selectlist option values, stores them in an array and post them to an Action Method

本文关键字:一个 存储 数组 Action Me Selectlist Jquery 获取 选项      更新时间:2023-09-26

需要帮助编写一个Jquery,获取所有Selectlist选项数据值并将它们存储在数组/集合中。这个集合将在Ajax调用中发送给Action方法,Action方法将返回一个json对象/集合,我将把这个对象传递给为onsuccess指定的函数。

由于服务器上的某些原因,Action方法没有接收到数据。然而,当我用调试器检查javascript时,数组"arr"被填充。

     public JsonResult GetPartPrice(int[] arr)
 {
     List<pidandprice> PidPriceList = new List<pidandprice>();
     foreach (var pid in arr.Where(x => x != null ))
     {
         var Product = context.Products.First(x => x.ProductID == pid);
         PidPriceList.Add(new pidandprice() { PartID = pid, Price = Product.ListPrice });
     }

      return Json(PidPriceList.Select(m => new {partid = m.PartID, price = m.Price}) );

}




<script type="text/javascript">

$(document).ready(function () {
    var arr = new Array();
    $('select option').each(function () {
        arr.push($(this).val());
    });



    $.ajax({
        type: "POST",
        url: "http://localhost:50913/Customise/GetPartPrice",
        data: arr,
        success: function (data) { OnSuccess(data) }
    });


});

function OnSuccess(data) {
    alert(data.join(', '));
    //This is where to write code to append
    //all option names text with price difference
};

免费下载:http://jsfiddle.net/DavidLaberge/PyGey/8/

对于ajax,您可能希望使用$。帖子,美元。Get,或者$.ajax