Search code examples
c#asp.netasp.net-mvcweb-applications

Dependancies between models in asp.net - supply object to another model


I'm studying asp.net MVC and i have difficulties to understand how models work together.

I have a HTML forms under a specific model, and i want user to select an object from another model with a listbox.

Let's say i have a model called "companies", which already have company A, company B & company C in database, that include Name, Address ... as properties.

Now, i have another model Loans, which is basically companies loaning to each other.

I built a HTML form where user can register a New Loan with different properties. Two of those properties are : "From Company" "To Company". User will have ListBox on those two properties, that shows list of Companies in Database : Company A, Company B...

What is the best way to do that ? How does models are linking to each other ? One company can have multiple Loans (Loaner or Borrower), but a Loan can only have One Loaner & One Borrower.

EDIT : I use EntityFrameWork & SQLite

Some code to start:

Models:

namespace App.Models
{
  public class Companies
     {
        public string Id{get;set;}
        public string Name{get;set;}
        public string Address{get;set;}
     }
}
namespace App.Models
{
  public class Loan
     {
        public string Id{get;set;}
        public int amount{get;set;}
        public string FromCompany{get;set;}
        public string ToCompany{get;set;}
     }
}

controller

namespace App.Controllers
{
    public class LoansController:Controller
    {
        private readonly LoansContext _context;

        public LoansController(LoansContext context){
            _context = context;
        }
    }
        [HttpGet]
        public async Task<IActionResult> LoansAdd()
        {   
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> LoansAdd([Bind("Id,fromEntity",
                                                        "toEntity",
                                                        "amount",
                                                         )]Loans loan)
        {
            if(ModelState.IsValid)
            {
                _context.Add(loan);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(LoansOverview));
            }
            return View(loan);         
        }
}

Html Form

@model App.Models.Loans
    <div class="col-md-4">
        <form asp-action="LoansAdd">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class = "col-md-6">
                        <div class = "form-group">
                            <div>
                            <label asp-for="Id" class="control-label"></label>
                            <input asp-for="Id" class="form-control"/>
                            <span asp-validation-for="Id" class = "text-danger"></span>
                            </div>
                            <div>
                            <label asp-for="fromEntity" class="control-label"></label>
                            <input asp-for="fromEntity" class="form-control"/>
                            <span asp-validation-for="fromEntity" class = "text-danger"></span>
                            </div>
                            <div>
                            <label asp-for="toEntity" class="control-label"></label>
                            <input asp-for="toEntity" class="form-control"/>
                            <span asp-validation-for="toEntity" class = "text-danger"></span>
                            </div>
                            <div>
                            <label asp-for="amount" class="control-label"></label>
                            <input asp-for="amount" class="form-control"/>
                            <span asp-validation-for="amount" class = "text-danger"></span>
                            </div>
     </div>


got a little bit lost with all solution i see, but i guess there should be properties that models share, like adding Loan properties on Company model


Solution

  • The simple way is to use a reference to refer to the companies :

    public class Loan
    {
        public string Id {get;set;}
        public int Amount {get;set;}
        public Company From {get;set;}
        public Company To {get;set;}
    }
    

    You can also add the loans to the companies:

    public class Company
    {
        public string Id{get;set;}
        public string Name{get;set;}
        public string Address{get;set;}
        public List<Loan> Loans {get;} = new List<Loan>();
    }
    

    And a method to make sure everything is created and added correctly:

    public static void CreateLoan(Company from, Company to, int amount){
        var loan = new Loan(){From = from, To = to, Amount = amount};
        from.Loans.Add(loan);
        to.Loans.Add(loan);
    }
    

    In real life you would likely use a database to store all the companies and loans, and something like Entity Framework to convert the database rows from and to objects. This adds a bit of complexity since you want to have some control of fetching and saving data to the database, but it would still model the relations between companies and loans in a similar way.