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.

  • there is a ViewBag c: - Buka
  • four
    Globality is moveton. For static information there are mechanisms for layouts ( partial views ) and partial views . - free_ze
  • @free_ze Layout.cshtml I found (connected via layout). I can call @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
  • one
    @free_ze duck classic mvc there. a person has a question how to pull out the same value from the database in each controller and each action, and how to display this so as not to repeat the code dozens of times - teran
  • one
    @free_ze duck here and the topic for the starter, he suggests the same thing, so he created a question. - teran

1 answer 1

Happened. Using layout (@free_ze helped a lot) you can call the controller method (which is associated with subd).

The controller supports Action, it was finalized as follows:

 public class MenuController : BaseController { [ActionName("GetCompany")] public System.Web.Mvc.MvcHtmlString GetCompany() { int id = 0; return new MvcHtmlString( int.TryParse(System.Web.HttpContext.Current.Request["c"]??"", out id) ? _repository.Companies.FirstOrDefault(t => t.Id == id).Name : "unknown"); } } 

Now I add to View from _layout.cshtml a call to GetCompany

 <a href="\" style="color: white">@Html.Action("GetCompany","Menu")</a> 

Accordingly, _layout can be obtained from the Layout property of the current page, usually at the top, for example, index.cshtml

  @model MyProj.WebUI.Models.ProductsListViewModel @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } 

Perhaps this is a blank that would just show how to add a property to all pages. For good, if you break a session, you need to do a redirect, and maybe something else can be finished.

It also helped. Why is the model not transmitted in View?

May Need ASP MVC Life Cycle

  • In C #, even arrays are allocated in the heap, so = new string[0]; Neither the fact that will not give due effect, but also will demand an extra allocation. But at List<T> , there is a constructor that allows you to reserve a buffer of the required size. - free_ze
  • 2
    And, sorry, it looks like a disgusting crutch: it cannot be tested, because MVC went to hell, even the view model knows about the http context, from where you extract the values ​​(!!!), the global state is used, the method called for all template views and this tin with locks, and yes even ViewBag dynamic, which is generally not recommended for use. This is a collection of antipatterns. - free_ze
  • In ASP.NET MVC, it was necessary to arrange it as a child action and call it directly, without this treshak. - free_ze
  • No statics! If you have List<Company> Companies in the model, why not take the name from there? - free_ze
  • You are already dragging a list of company objects within the model; here you could filter it along the way: Companies.First(c => c.Id == id).Name . - free_ze