Utilizing a List<> as a template - asp.net mvc

When I return a view with a List as a model, the code looks like this:

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. While this code compiles, is it advisable to do so?
  2. I need to interact with this list using JavaScript. I have previously obtained lists in JSON format through AJAX calls on different pages but can't do so here. In this scenario, how can I work with the list passed in through JavaScript, using the following method:

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

The list referred to as prepaymentList is the one being mentioned.

Answer №1

To convert the model into a JSON object, you can utilize the JavaScriptSerializer in JavaScript:

<script type="text/javascript">
    var dataList = <%= new JavaScriptSerializer().Serialize(Model) %>;
    // Here is where you can use the data list, such as passing it to a function:
    ShowDataChart(
        dataList, 
        "TotalValueString", 
        "#valueChart", 
        "Data Values"
    );
</script>

Answer №2

  1. Absolutely, that is possible.

    Here's an example code snippet: myList = ViewData["data"] as List<Analysis.PrepaymentResult>;

  2. When it comes to serialization, this information will be transformed into a JSON Array making it easy for you to iterate over. As for the concept of "passing in through javascript", could you clarify if this pertains to data transmitted via an Ajax call?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize Ajax to automatically populate a textbox with suggestions

I'm retrieving data via an AJAX call. How can I bind the data for auto-completion in a text box using both the name and ID as fields? What is the best way to bind this data in the frontend and retrieve the selected name's ID in the backend using ...

Guidelines for implementing a vuex getter within the onMounted hook in Vue

Currently, my process involves fetching data from a database and storing it in vuex. I am able to retrieve the data using a getter in the setup method, but I would like to manipulate some of that data before the page is rendered, ideally in the onMounted m ...

Making changes to a variable or option through the props in a different file

In the index.js file, I have implemented getStaticProps() to fetch data from an exported function. This function requires 2 parameters, one for the type of listing and the quantity. export async function getStaticProps() { const type = 0 const data = a ...

Style of LINQ method syntax

It has come to my attention that the LINQ queries written by each programmer on our team vary greatly in style, making them difficult to read and creating an unattractive codebase. The discrepancies are especially noticeable when multiple styles exist with ...

The Trouble with Vue.js 2 and Axios Scopes

I previously encountered this "problem" but can't recall the correct approach to achieve the desired results. My current setup involves using Vue 2 to load data into variables that are then displayed on the HTML side: window.Vue = require('vue&a ...

When downloading files that I uploaded to Google Drive using React Native, I noticed that the file size is not consistent with the original file

Greetings Everyone! I am aiming to transfer the database (RealmJS) that is currently stored on my device storage to Google Drive using Google Drive API V3. To accomplish this, I have already installed various node modules such as react-native-fs and @reac ...

What steps should I take to address both the issue of duplicate names and the malfunctioning fixtures?

There are issues with duplicate item names and the cache not updating immediately after running the script. Instead of fetching new data, it retrieves previous values from the last item shop sections. If the remove_duplicates function is not used, it displ ...

Having trouble with the functionality of Angular 2 filter and toggle features?

I recently developed a hamburger menu with a toggle function to display subcategories. Everything was working smoothly until I applied a filter. Once I search for a category using the filter, the toggle function stops working. Here is a snippet of my HTML ...

What is the reason behind caching form data in an ajax call?

After the first successful submission, I am facing an issue where the original form data is being re-sent even when new values are submitted for a second attempt. How can I reset and reload the data to avoid this problem? I have tried several approaches i ...

Encountering Flow type errors when declaring global variables in React

type Props = { checkedItems:any }; class MyApp extends Component<Props> { constructor(props) { super(props); this.checkedItems = []; } } In this code snippet, I am utilizing Flow for type checking within a React class component. However ...

Issue with React setState not triggering when switching between tabs in Material UI. Attempted to hide using a div with display set to none

Every time I switch between Material UI Tabs, the state gets cleared and does not persist. Check out this link for more information I have attempted different solutions: Using React Context to pass in the React State, but the issue remains unresolved T ...

Exploring Selenium with C#: How to continuously loop until an element is no longer on the screen

Working with Selenium and C# to run a series of tests, I encountered an issue that has me stuck. In one scenario, a report is generated and a spinner control is displayed on the page while the data is being processed. Once the process is complete, the spin ...

Displaying a dynamic progress bar across all elements in fullscreen mode

I am looking to implement a full-screen indeterminate progress bar that overlays all screen elements. Here is my specific use case: When a user fills out a form, including an email field, the email id is checked against a database via ajax. While waiting ...

Is there a way to refresh or reload the app.config file?

I am trying to retrieve the value from app.config, display it in a message box, update the value using an external text editor, and then show the updated value. My attempt at achieving this was through the following code snippet: private void button2_Cli ...

Reading console input in debug mode for a dotnet core Web API application is a vital

I recently started learning dotnet core programming. While working in Visual Studio in debug mode, what is the method to view messages printed to the console using the Console.WriteLine("Test Message") function? ...

Is it possible to create a hyperlink on the second page that directs to the first page and triggers a specific JavaScript function on the first page?

Working with a former colleague's code in C#, asp.net MVC, Telerik/Kendo controls. I am trying to navigate to a specific section on a page from another page that uses tabs/sections with links at the top of the page to toggle visibility based on the se ...

Connecting Formik with the react-phone-number-input library: A step-by-step guide

I am currently working on developing a form using Formik, Material UI, and the React-phone-number-input library for phone number formatting. I have encountered an issue where the formatted phone number is not being inserted into the Formik state, causing F ...

Creating a Kendo UI grid within a Kendo UI window and utilizing Knockout JS bindings

I'm encountering an unusual issue while working with Kendo UI and Knockout JS libraries. When attempting to display a kendo grid within a kendo window, the rows inside the grid are being duplicated. Below is a snippet of the code I'm using: JS: ...

Modifying HTML files in a Node.js environment with the fs module

Struggling with learning node.js and back-end development, I encountered a problem that I can't solve. Can anyone lend a hand? I've been working on changing my HTML page from h1.html to h2.html, but I keep encountering the following error: (Error ...

What is the best way to retrieve a byte array within a function that accepts generic parameters in C#?

I am looking to pass int, short, Single, or Double to a function. The function should return an array in reverse order. For example: fun(2.0f) should return [0x40, 0, 0, 0] fun(0x01020304) should return [1, 2, 3, 4] fun((short) 0x0102) should retu ...