简体   繁体   中英

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM