gpt4 book ai didi

c# - .Net 将对象列表作为参数传递给 JQuery

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:21 26 4
gpt4 key购买 nike

您好,我试图将一个自定义对象列表作为参数传递给 JQuery,并希望 jQuery 将其识别为特定对象并循环遍历它。该类是:

public class PriceSummary{
public string ItemName { get; set; }
public string ItemPrice { get; set; } }

后端方法是:

public void updatePriceSummary(List<PriceSummary> PriceSummaryList){
ScriptManager.RegisterStartupScript(Page, GetType(), "changePriceList",
"updatePriceList('" + PriceSummaryList + "');", true);}

JQuery 方法

function updatePriceList(PriceSummaryList) {
PriceSummaryList.each(function ()
{$('#ControlSummaryTitle').append('<tr id = "xxx"> <td class="SummaryItem">' +
this['ItemName'] + '</td> <td class="SummaryPrice">' + this['ItemPrice'] +
'</td></tr>'); })
;}

但是,JQuery 函数无法将参数识别为列表并且无法循环遍历它。错误信息是:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'each'

有人可以建议如何修复它吗?

最佳答案

问题 1:如果您只是将一个 List<> 组合成一个字符串,它将通过 .Net 知道的最简单的方法变成一个字符串,即 .ToString(),导致“System.Collections.Generic。列出`1[someclass]”。您需要将其转换为 Javascript 可以理解的内容,比方说将其序列化为 JSON。您可以轻松地检查浏览器中的值,看看您是否正在接近可行的东西。

问题 2:您用单引号 (updatePriceList('...')) 将值括起来,因此它仍会被解释为字符串;删除单引号。

关于c# - .Net 将对象列表作为参数传递给 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43129469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com