I'm building a MVC 5 app that deals with logging time for employees among other things. I already have a complete CRUD controller with views for dealing with employee records and I have a Timesheet controller for entering time for employees.
I would like to add shortcuts to the Timesheets/Index view to get access to the Employees module without going through the Admin/Index view. I would also like to directly access the Employee Edit methods from a subordinate view to Timesheets/Index.
In the spirit of DRY, can I reuse the Employee controller logic and still get back to where I came from or do I need to duplicate the Employee logic to call different views with different action links? (I know I can reuse part of the view code by using partial templates but that doesn't go far enough.)
OK from the responses I did not do a very good job of explaining what I want to do. Here is part of the Employees controller code:
public partial class EmployeesController : Controller
{
//
// GET: /Employees/
public virtual ActionResult Index(int? page)
{
const int pageSize = 15;
var masterDataProxy = MasterDataChannelFactory.OpenChannel();
var employees = masterDataProxy.GetPagedEmployees((page ?? 0) * pageSize, pageSize);
masterDataProxy.CloseChannel();
ViewBag.HasPrevious = employees.HasPrevious;
ViewBag.HasMore = employees.HasNext;
ViewBag.CurrentPage = (page ?? 0);
return View(employees.Entities);
}
//
// GET: Employees/Edit/{id}
//[Authorize(Roles = "Admin")]
public virtual ActionResult Edit(int id)
{
var masterDataProxy = MasterDataChannelFactory.OpenChannel();
var employee = masterDataProxy.GetEmployee(id);
masterDataProxy.CloseChannel();
return View(employee);
}
//
// POST: Employees/Edit/{id}
[AcceptVerbs(HttpVerbs.Post), /*Authorize(Roles = "Admin")*/]
public virtual ActionResult Edit(int id, FormCollection formValues)
{
var masterDataProxy = MasterDataChannelFactory.OpenChannel();
var employee = masterDataProxy.GetEmployee(id);
masterDataProxy.CloseChannel();
if (null == employee)
{
return View(Views.NotFound);
}
try
{
UpdateModel(employee, formValues.ToValueProvider());
var adminProxy = AdminChannelFactory.OpenChannel();
adminProxy.AddUpdateEmployee(employee);
adminProxy.CloseChannel();
return RedirectToAction(Actions.Index());
}
catch (Exception ex)
{
ModelState.AddModelError("Employee", ex.Message);
return View(employee);
}
}
...
}
Here is part of the Admin index page view:
@{
ViewBag.Title = "Master Data Admin";
}
<h2>Master Data</h2>
<ul>
<li>@Html.ActionLink("Accounts", MVC.Account.Actions.Index())</li>
<li>@Html.ActionLink("Employees", MVC.Employees.Actions.Index())</li>
</ul>
And then I have a Timesheet/Index view where I want to add another ActionLink to Employees. My question is simply how do I write this so I can call into the Employees Controller from either view (Admin/Index or Timesheets/Index), update the Employees on the service, then return back to where I was called from?
It seems this should be a solved problem but I couldn't find anything close to what I want to do. Maybe I need to rephrase the question? I shoudl add I'm a relative newbie to MVC and web programming in general...
Thanks in advance for any help or guidance.
Dave