Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use MVC controller from multiple places
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>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.)</p> <p>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:</p> <pre><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); } } ... } </code></pre> <p>Here is part of the Admin index page view:</p> <pre><code>@{ ViewBag.Title = "Master Data Admin"; } &lt;h2&gt;Master Data&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;@Html.ActionLink("Accounts", MVC.Account.Actions.Index())&lt;/li&gt; &lt;li&gt;@Html.ActionLink("Employees", MVC.Employees.Actions.Index())&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>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?</p> <p>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...</p> <p>Thanks in advance for any help or guidance.</p> <p>Dave</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload