A
A
Andrey Golubkov2017-01-09 11:45:10
HTML
Andrey Golubkov, 2017-01-09 11:45:10

How to pass data to Layout from Html.RenderAction?

Here's how I imagine it, but this option does not work:
15cca0fe631a40e995d2539434749510.png
Data is transferred to GetUser.cshtml, but there is no data from GetUser to layout, an empty object comes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Telmanov, 2017-01-09
@Android97

Oh youth!
Well, if it comes to that, why can't the user be transferred to the view from the controller in another way?
Create a view model, make CurrentUser property in it and put it there, and then call @Model.CurrentUser
And if we say how to do it right, then you can do this:
1) Create classes BaseViewPage BaseViewPagelt;T gt; inherit from WebViewPage and WebViewPage lt;T gt;
2) Then add public property User to both implementations of the BaseViewPage class
3) Make a service to pull out the current user
(for example, you take his email or id from the session, cling to it from the database and give it back)
4) Call the service from BaseViewPage and BaseViewPage lt ;Tgt;
You create a service instance there, or inject it there, as you like.
Then you get the user.
For example, Public User CurrentUser {get{ return AuthServise.GetCurrentUser();}}
5) change lt in the view config
; pages pageBaseType="System.Web.Mvc.WebViewPage" gt;
to
lt; pages pageBaseType="BaseViewPage" gt;
And you will have in any view everywhere User current, happiness!

F
Fat Lorrie, 2017-01-09
@Free_ze

You have the sequence of pictures mixed up: Layout calls the action of the Auth controller, and that, in turn, calls the GetUser view and inserts it at the call site in Layout (1 - 3 - 2)
However, ViewBagthis is an individual property of each controller ( BaseController). That is, they are different for the Auth controller and the one that calls Layout (let's call it X) and your idea will not work.
You are trying to pull out the business logic on the views.
This needs to be received not with the help of a separate action and the Auth controller view, but from the service (in which to wrap the work with the database). Then drag it through the view model of the X controller action.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question