Back to previous page in ASP.Net MVC

Last Reply 6 months ago By chetan

Posted 6 months ago

Phase goes like this: Department list---> Employee List--->Details of  Employee

I have the Following Department Controller code:

 public class DepartmentController : Controller
{
    public ActionResult Index()
    {
        EmployeeContext employeeContext = new EmployeeContext();
        List<Department> departments = employeeContext.Departments.ToList();
        return View(departments);
    }
}

View(index.cshtml of Department):

@model IEnumerable<department>
@using Kuduvenkat_MVC_3_MT.Models;
<div style="font-family:Arial">
    @{
        ViewBag.Title = "Departments List";
    }

    <h2>Departments List</h2>
    <ul>
        @foreach (Department department in @Model)
        {
            <li>
                @Html.ActionLink(department.Name, "Index", "Employee",
    new { departmentId = department.ID }, null)
        </li>
        }
    </ul>
</div>

I have the Following Employee Controller code:

 public class EmployeeController : Controller
{
    public ActionResult Index(int departmentId)
    {
        EmployeeContext employeeContext = new EmployeeContext();
        List<Employee> employees = employeeContext.Employees.Where(emp => emp.DepartmentId == departmentId).ToList();

        return View(employees);
    }

    public ActionResult Details(int id)
    {
        EmployeeContext employeeContext = new EmployeeContext();
        Employee employee = employeeContext.Employees.Single(x => x.EmployeeId == id);

        return View(employee);
    }
}

View(index and details.cshtml of Employee)

@model IEnumerable<Kuduvenkat_MVC_3_MT.Models.Employee>
<div style="font-family:Arial">
    @{
        ViewBag.Title = "Employee List";
    }
    @using Kuduvenkat_MVC_3_MT.Models;

    <h2>Employee List</h2>
    <ul>
        @foreach (Employee employee in @Model)
        {
            <li>@Html.ActionLink(employee.Name, "Details", new { id = employee.EmployeeId })</li>
        }
    </ul>
</div>

 

<h4>Employee</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Name)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Name)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Gender)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Gender)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.City)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.City)
    </dd>

</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeId }) |
@Html.ActionLink("Back to List", "Index")
</p>

My issue is Back to list Actionlink of Details.cshtml.It gives me error as it does not go to previous page. Any help will be appreciated.


Posted 6 months ago Modified on 6 months ago

This is what i was looking For.

Got the solution.

 

@Html.ActionLink("Back to Employee List", "index", new { departmentId = @Model.DepartmentId })