There is a project on ef + mvc. With beautiful controller + 30 models + n-view It was required on all pages (Views) to write the name of the company. It turns out ... you need to make changes in all models, and not just changes, but also link them to the repository (IRepository), as the company name is stored in IQueryable<Company> companies from the repository-DBMS (some models are not connected) Is there no easier way? Do you need to do everything through the global model ... or is there a globality mechanism for such cases? Perhaps this should not be done through the model ...
Now in more detail. The database is connected via EF, it is connected to the controllers through the designer, for example
public class HomeController : BaseController { private IProductRepository _repository; public HomeController(IProductRepository productRepository) { this._repository = productRepository; } } Part of the view is connected to separate tables of the IQueryable repository. The firm is selected on the main (Home) page. I implemented it like this:
@model MyProj.WebUI.Models.ProductsListViewModel @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ... @foreach(var item in Model.Companies){ <h1 style="font-family:'Lobster';color:white"> <a style="font-size:x-large;color:blue;" href="/Menu?c=@item.Id">@item.Name</a> </h1> } Since users can choose any company, I did not encrypt the id or make a post-request (this is not so important, it will get into the Request anyway).
I set myself a task. I have the main menu page. After selecting the "company" I need to display its name on other pages. I will keep the name of the company in Request["c"] . Next, a little about the decision. I will check if the model supports the Companies property, if yes - I save the name and id of the company, then, from the "cache" I will display them. Most likely through ViewBag.
I don’t know if this problem can be solved in a “general” form. I have already "planned" a decision for myself, I will publish it later. I think someone else will face a similar task.
@MyProj.f(x)from layouts where x is either HttpContext or Model, but they do not contain an IRepository, how to get to the base so that not all models can be sculpted with DataSets or connections? - nick_n_a