简体   繁体   中英

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

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();
}

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});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM