Asp.net mvc: How to fill dropdownlist from another database data

ben salem samira 1 Reputation point
2021-10-16T17:57:24.783+00:00

Hello

I am a beginner in the development of asp.net web applications

I am asked to build a solution for the visitor management system, so i have prepared my buisness model classes that are:

  • visitor,
  • visite,
  • employee,
  • Department

a visit class has properties witch are:

  • Id (primary key)
  • Visite Date
  • Visite Hour
  • VisitorId (Foreinkey 1)
  • EmployeeId (Foreinkey 2)
  • DepartementId (Foreinkey 3)

I have created a database to store visits data and visitor data.

To create a visitor object, we need to implement a html form in my asp.net mvc project view that containes dropdownlists for the foreinkey properties.

the Employee and Departement tables are already existe in Rh database.

So my question is:

Is there a way to get data from the Rh database to populate the EmployeeId and DepartmentId drop-down lists without re-creating the employee and Department table in the visit database?

Developer technologies .NET Other
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-10-17T07:30:10.24+00:00

    You can have two connections in the solution with each one going to two different databases.

    You need to understand SoC in regards that you do not have direct database access in the controllers. The database access needs to happen elsewhere not in the controller IMO.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    You should read the subject below in the below link. What it is basically saying is make classes in the Models folder for database access. Methods in the controller will call methods in classes in the Models folder to do the database access on the behalf of the controller, which is a form of SoC.

    Understanding Models

    https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    You should understand the viewmodel. The viewmodel is what is passed into a view that has been populated from a database persistence model populated from database access. In turn, the viewmodel populates the database persistence model with its data persisted to the database.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    Understand the various models.

    https://deviq.com/terms/kinds-of-models

    here is some example code on what I am talking about look at the MVC project and see how things work. You can create a DAL that is accessible by the classes in the Model folder directly. The DAL I have in the project is used by a WebAPI in the solution that is more complicated than what you need to be doing but understand the DAL concept you can use it.

    VM = viewmodel and DM = domain model.

    https://github.com/darnold924/PublishingCompany

    DAO pattern in the DAL being used DTO pattern is being used.

    https://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    got questions ask...

    HTH

    0 comments No comments

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2021-10-18T07:31:40.637+00:00

    Hi @ben salem samira ,
    You can directly access Rh database, and you can specify multiple connection strings in one configuration. You can pass different connection strings to the DBContext constructor. Usually, your DAL/data access layer or repository layer will extract the appropriate connection string from Web.Config/App.config and pass it to the DBContext constructor.
    You need to create multiple DbContext, you can associate with a group of entities in the database in the context.

    public class employeeContext : DbContext  
    {  
        public employeeContext ()  
            : base("connectionStringA")  
        {  
        }  
        public DbSet<employee>  EmployeeId { get; set; }  
    }  
    public class DepartmentContext : DbContext  
    {  
        public DepartmentContext ()  
            : base("connectionStringB")  
        {  
        }  
        public DbSet<Department>DepartmentId{ get; set; }  
    }   
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.