开发者

Using a List<> as a model - asp.net mvc

开发者 https://www.devze.com 2023-02-10 05:41 出处:网络
So I am returning a view with a List as a model as such: List<Indications.Analysis.PrepaymentResult> resultsList = Indications.Analysis.PrepaymentResult.GetPrepaymentResult(indication.Mo开发者_

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

List<Indications.Analysis.PrepaymentResult> resultsList = Indications.Analysis.PrepaymentResult.GetPrepaymentResult(indication.Mo开发者_如何学编程del.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.


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>


  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?

0

精彩评论

暂无评论...
验证码 换一张
取 消