Home > Mobile >  Is there any way to pass the data from one controller to another in ASP.NET MVC?
Is there any way to pass the data from one controller to another in ASP.NET MVC?

Time:01-03

public IActionResult LoginSubmit(Employee obj)
{
    var abc = _db.Employees.Where(x => x.EmailId == obj.EmailId).FirstOrDefault();
    var passAuth = _db.Employees.Where(x => x.Password == obj.Password).FirstOrDefault();

    if (abc != null && passAuth != null)
    {
        return RedirectToAction("Index", "EmployeeLeaves");
    }
    else
    {
        return RedirectToAction("Login","Employee");
    }
}

The code above is my employee controller and I want to pass the id to the Employeeleaves controller (shown below) so that I can use it and fetch data from both the tables.

Also the login authentication condition is pretty bad. Please suggest the logic for that also. Is there any way to then fetch data from both the tables in EmployeeLeavesController using LinqJoin

private ApplicationDbContext _db;

public EmployeeLeavesController(ApplicationDbContext db)
{
    _db = db;
}

public ViewResult Index(int EmployeeId)
{
    //var employeeLeaveEMp = _db.EmployeeLeaves.Include(c => c.Employee).ToList();
    //List<Employee> employeeList = _db.Employees.ToList();
    //List<EmployeeLeaves> employeeLeaves = _db.EmployeeLeaves.Where(emp => emp.EmployeeId == EmployeeId).ToList();

    return View();
}

CodePudding user response:

If you just want to pass EmployeeId after user logged-in, you can pass it like below:

return RedirectToAction("Index", "EmployeeLeaves",new {EmployeeId=abc.Id});

CodePudding user response:

As far as passing value from controller to another is concerned. You can use a session variable to store the value and then use it in the other controller. And yes you can fetch records from both tables using join just give a try looking and JOIN queries in LINQ

https://www.tutorialsteacher.com/linq/linq-joining-operator-join

  • Related