1
\$\begingroup\$

I have created a code that displays a table with exchange rate data. Have I used loops and conditions (if else) in a good way?

@model IEnumerable<CurrencyResponse>
@{
 ViewData["Title"] = "Index";
}
@foreach (var obj in Model)
{
 <div>
 <h5>Table: @obj.Table</h5>
 <h5>Table number: @obj.No</h5>
 <h5>Date of publication: @obj.EffectiveDate</h5>
 @if (obj.Table == "C")
 {
 <h5>Quotation date: @obj.TradingDate</h5>
 }
 </div>
}
<table class="table table-bordered table-striped" style="width: 100%">
 <thead>
 <tr>
 <th>
 Currency
 </th>
 <th>
 Code
 </th>
 @foreach (var obj in Model)
 {
 @if (obj.Table != "C")
 {
 <th>
 Mid
 </th>
 }
 else
 {
 <th>
 Bid
 </th>
 <th>
 Ask
 </th>
 }
 }
 </tr>
 </thead>
 <tbody>
 @foreach (var obj in Model)
 {
 foreach (var element in obj.Rates)
 {
 <tr>
 <td width="50%">
 @element.Currency
 </td>
 <td width="50%">
 @element.Code
 </td>
 @if (obj.Table != "C")
 {
 <td width="50%">
 @element.Mid
 </td>
 }
 else
 {
 <td width="50%">
 @element.Bid
 </td>
 <td width="50%">
 @element.Ask
 </td>
 }
 </tr>
 }
 }
 </tbody>
</table>

As you can see, I'm using 4 loops to display the data the right way. I am using conditions to add extra html. I wonder if what I'm doing is right. Can it be somehow more optimized? In the created div I use h5 tags to display information about the table (A, B or C) and information about the date of publication of the exchange rates.

xml structure - (tables A, B, C):

endpointA

endpointB

endpointC

In case of ambiguity, I also attach all the code to the github: code

Toby Speight
87.5k14 gold badges104 silver badges322 bronze badges
asked Aug 31, 2022 at 12:55
\$\endgroup\$
1
  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$ Commented Aug 31, 2022 at 13:07

1 Answer 1

1
\$\begingroup\$

I'm not too familiar with MVC best practices, but I have some general suggestions.

First, if (obj.Table != "C") is pretty meaningless to a new reader. What is the actual condition being tested for here? Can you encapsulate this test in a function with a suggestive name?

Second, you should prefer using css classes to assigning styles like width directly to elements. You just have to decide how the styles should be scoped in your case - maybe with a class on the table.

answered Sep 2, 2022 at 18:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.