使用列表<>作为模型 - asp.net mvc

发布于 2024-10-17 12:20:44 字数 677 浏览 0 评论 0 原文

所以我返回一个带有列表作为模型的视图:

List<Indications.Analysis.PrepaymentResult> resultsList = Indications.Analysis.PrepaymentResult.GetPrepaymentResult(indication.Model.Trx, indication.Model.ShockBpsDropList.Value, indication.Model.ShockIncrements.Value);
return View(@"~\Views\Indications\TermSheetViews\Swap\PrePayment.aspx", resultsList);
  1. 这可以编译,但是,我可以这样做吗?
  2. 我需要在 javascript 中处理这个列表,我在另一个页面上有代码从 AJAX 获取 Json 列表,但在这种情况下我没有能力做到这一点。然后我将如何使用通过 javascript 传递的列表,使用以下方法:

    CreateShockTable(data.pre paymentList, "TotalValueString", "#valueTable", "Pre payment Value");

pre paymentList 就是这个列表。

So I am returning a view with a List as a model as such:

List<Indications.Analysis.PrepaymentResult> resultsList = Indications.Analysis.PrepaymentResult.GetPrepaymentResult(indication.Model.Trx, indication.Model.ShockBpsDropList.Value, indication.Model.ShockIncrements.Value);
return View(@"~\Views\Indications\TermSheetViews\Swap\PrePayment.aspx", resultsList);
  1. This compiles but, can I do this?
  2. I need to work with this list in javascript, I have code on another page that gets the list in Json from AJAX but in this case I don't have the ability to do that. How would I then work with the list that I am passing in through javascript, with the following method:

    CreateShockTable(data.prepaymentList, "TotalValueString", "#valueTable", "Prepayment Value");

That prepaymentList is this list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-10-24 12:20:44

您可以使用 JavaScriptSerializer 将模型序列化为 JSON 对象

<script type="text/javascript">
    var prepaymentList = <%= new JavaScriptSerializer().Serialize(Model) %>;
    // TODO: use the list here, for example pass it to some function:
    CreateShockTable(
        prepaymentList, 
        "TotalValueString", 
        "#valueTable", 
        "Prepayment Value"
    );
</script>

You could serialize the model into a JSON object using JavaScriptSerializer:

<script type="text/javascript">
    var prepaymentList = <%= new JavaScriptSerializer().Serialize(Model) %>;
    // TODO: use the list here, for example pass it to some function:
    CreateShockTable(
        prepaymentList, 
        "TotalValueString", 
        "#valueTable", 
        "Prepayment Value"
    );
</script>
顾冷 2024-10-24 12:20:44
  1. 是的,可以。

    var myList = ViewData["whatever"] as List;

  2. 它在序列化过程中被转换为 JSON 数组,因此您可以轻松地循环它。
    我不太明白“通过javascript传入”是什么意思。它是通过 Ajax 调用的操作的结果吗?

  1. Yes, you can.

    var myList = ViewData["whatever"] as List<Indications.Analysis.PrepaymentResult>;

  2. It is translated into JSON Array during serialization, so you can easily loop through it.
    I don't clearly understand what do you mean by "passing in through javascript". Is it a result of the action called through Ajax?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文