The viewdata dictionary viewdata is of a data type

This preview shows page 18 - 20 out of 87 pages.

The ViewData dictionary ViewData is of a data type derived from the C# Dictionary class. o Dictionary in C# is very similar to English dictionary. o English dictionary is a collection of words and their definitions. o Dictionary in C# is a collection of Keys and Values, where key is like word and value is like definition. The Key in a C# dictionary is typically a string but can be other types as well. The Value in a C# dictionary can be of any type. In PageModel file, the ViewData is filled with the keyed data. o You can introduce an arbitrary string as a key In content file, the data is retrieved by referencing the key This will display the 'message' for 'onemore' times in the user's web browser. NB: The lifespan of ViewData only lasts one HTTP request/response. o It starts in the PageModel file and finishes after rendering the content file. o It cannot be remembered across different HTTP requests (or web pages). For example, the ViewData created in the PageModel of a Razor page cannot be used by the PageModel of another Razor page. Remarks for coding a PageModel file For simplicity, the code for user input sanitization is omitted in this example. o You'll see later in this unit that the input sanitization can be handled automatically by the framework. o Recall: input sanitization is necessary for preventing XSS attacks, etc. The OnPost() method can be programmed similarly.
Actually, the Visual Studio can automatically generate source code for OnGet() and OnPost() in many scenarios. o You'll see this in the coming lectures. However, it is still important that you know to how to code them manually, so that you understand what is happening under the hood. Debugging Overview In programming, bugs are inevitable, so debugging is a key skill you must have. The 'Debug' menu in VS makes debugging extremely easy . The ‘Debug’ menu of VS: F5 vs Ctr+F5 When you hit F5 or click the 'IIS Express' button in the toolbar of VS, you run your application in debugging mode. o Allowing you to set a breakpoint at a line of code and your application will stop when it hits the breakpoint. o Allowing you to step through each line of code after the breakpoint. o Showing you the values of the variables when you step through the code. When you hit Ctrl+F5, you run your application without debugging. o It uses much less system resources. o When you just test run your app, you should use this mode. The ‘Debug’ menu: F9 for breakpoints Click the line of code where you want to set a breakpoint. Hit F9 or click the 'Toggle Breakpoint' menu item to set the breakpoint Hit F5 to run your app with debugging, and the app will start to run and stop at the breakpoint. Examine the values of variables when the app stops at the breakpoint.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture