Need to write a Jquery that gets all Selectlist option values, stores them in an array and post them to an Action Method
Need help to write a Jquery that gets all Selectlist option data values and stores them in an array/collection. This collection will then be sent in Ajax call to an Action Method, the action method will then return a json object/collection and I will pass this object to the function specified for onsuccess.
For some reason on the server the Action method receives no data. The array "arr" is however populated when I check t开发者_C百科he javascript with a debugger.
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
};
Here's a freebe : http://jsfiddle.net/DavidLaberge/PyGey/8/
for your ajax you may want to use $.post, $.get, or $.ajax
精彩评论