I see here: https://github.com/santosh-kumar-metikoti/Expense-Manager/blob/main/WebApplication4/Controllers/HomeController.cs#L37 and in other places that you have chosen to return a data set or a data table.
While this is OK, a better approach would be to return a model that is strongly typed instead. For example, in the Index()
action of the controller, you want to return a list of transactions, so I would choose to do something like this:
public class TransactionsViewModel
{
public List<Transaction> Transactions {get; set;}
}
public class Transaction
{
public string Account { get; set; }
// other fields
}
and populate Transactions
accordingly based on what you retrieve from the Data Set.
The advantage with that is that the contract between your action and your view is a strict contract at this time. This means that any change on either side will throw compiler errors. Currently you are using: https://github.com/santosh-kumar-metikoti/Expense-Manager/blob/main/WebApplication4/Views/Home/Index.cshtml#L45-L53 a string in order to retrieve information from each row (@row["account"]
) - this is error prone because if someone misspells the word account
then we will only know at runtime instead of at compile time.
In the case of using a model, this would look like:
Which is checked at compile time โ๏ธ
Please change all your actions to return Models instead of returning Data Table / Data Set
Note
There will be an additional looping through the data set / table in order to populate the list, but we will deal with that shortly.