I have an MVC 4 based web app. Where I provide 2 login types, 1. Employee and 2. Customer.
With Customer login, I present a dashboard and other stuff, about his orders, etc.
With Employee login, I allow the employee to search for his customers and provide the ability to carry out certain customer specific tasks.
It is possible that, an employee after login can seach for a number of customers and fetch each of their details in multiple browser tab(s).
I am having difficulty in maintaining state for each tab, if I put the unique identifier of the customer in session, as the next subsequent request will overwrite the session!
To solve this, I have relied on a (dirty) viewbag based solution, where I set the customer number in viewbag in every view.
Is there an elegant solution to solve this? a known design pattern?
0
Put the customer identifier in the URL, a la: http://yourApp/Customers/990023
(Custom ID = 990023)
Expanded Answer:
First, register the URL parameter for the Customer ID in your RouteConfig.RegisterRoutes()
method (found in RouteConfig.cs). It might look something like this:
routes.MapRoute(
name: "Customer",
url: "Customer/{action}/{id}",
defaults: new { controller = "Customer", action = "Index", id = UrlParameter.Optional }
);
If every method in the Customer Controller requires the ID, that is, if every action requires that you’ve got a valid customer loaded, then you can remove the id = UrlParameter.Optional
part.
Now, your methods in the Customer Controller will be looking for a URL segment at the end of the URL that maps to a parameter called id
. You can access it in your methods like so:
// GET: /Customer/MakePayment/990023
public ActionResult MakePayment(string id)
{
var makePayVModel = new MakePaymentViewModel();
makePayVModel.LoadModelData(memberNumber);
return View(makePayVModel);
// or do whatever you want here.....
}
Finally, here’s a tip. When you are dealing with ActionLinks and the like, say from one customer View to another, make sure you pass in the ID as part of the Route Values. So if your ViewModel for that View has a property called “Id” that represents the Customer Id, you’d make your link might look something like this:
@Html.ActionLink("Make a Payment", "MakePayment", new { id = Model.Id } )
Which would render a tag like so: <a href="http://yourApp/Customer/MakePayment/990023">Make a Payment</a>
1