4

My aplication have walls with notes. I wish to put on layout name of active wall. How to do that? I know that on front-end I can use just @Model... but who sends this model? Also better idea is store this information in session variable or create another property in user database?

EDIT: I use .NET Core 1.1.

1

2 Answers 2

6

If you are talking about the _Layout.cshtml then you could place the following in _Layout.cshtml:

@RenderSection("layoutName", required: false)

Any view which inherits this layout can now add code to this section.

SomeOtherView.cshtml:

@section layoutName{
   <h1>@Model.LayoutName</h1>
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but how to keep this text if i change view?
What the OP wants is to directly control that at Layout level, instead of forcing to use a SECTION on every view that requires it, he wants to decide that at layout level with a conditional. It is simpler to maintain because you only need to change that at Layout rather than all views that use it. Think of something to add the necessary scripts for using the GoogleMap API.
that may be so, but a conditional statement could grow into something massive depending on the number of views he has. What has googlemap API got to do with this issue?
4

How to pass variable into layout in .NET?

I know that on front-end I can use just @Model... but who sends this model?

Model is created in the controller/action, and you are passing it to view from your action:

return View(myModel);


For the more accurate answer you need to specify the question or add example code of what you are trying to do, but in general case, you could use:

  • Model (ViewModel)
  • ViewBag
  • ViewData


Please find more information:


Also, View Components is an interesting feature which allows you to create UI widgets.


Update:

In standard layout after login you can see for example "hello a@a" all the time. I wish to have this same with my active wall. Have on top screen name of my wall until I change it or i log out.

For this particular purpose, you could just put the following code directly into your layout/view file:

@using System.Security.Claims
@using Microsoft.AspNetCore.Identity
@inject UserManager<ApplicationUser> userManager
@{
    var userInfo = ((await userManager?.GetUserAsync(User))?.xxx);
    // where 'xxx' is any property of ApplicationUser model
}

then you can use @userInfo in the same view to display that info.

User choose wall (just click on the same wall from the list) then name of this wall should be on layout until user log out or change wall.

In this case, JavaScript seems to be a good/easy solution? Or you could read/store that information in browser cache?

As far I'm aware there is no easy way to access session in ASP.NET Core - controller actions are designed to work as stateless, and it's not recommended to override that behaviour (read docs and SO why?).

So if not JavaScript then you could send back the name of a wall to the server and store it in DB, and next you could access it anytime via View Components - I'm using ViewComponents in my project to build and display the main menu, and it's work well for me.

There is a little bit more coding for ViewComponent (you need to create controller and view), but it's flexible feature (I like it) and easy to call in a place where you need it, just

@await Component.InvokeAsync("NameOfCOmponent").

5 Comments

Ok but how to then store this variable? If i use viewBag or pass model in controller i will see my text. But if i click into next tab, my viewbag contains Null. So how to store this variable?
That is what I do not understand - what and why you want to store? Please, could you give me more details?
In standard layout after login you can see for example "hello a@a" all the time. I wish to have this same with my active wall. Have on top screen name of my wall until I change it or i log out.
I will give a full answer today later (it's hard to write from phone), but quick question - from where you're getting the name of a wall and when/how it's changing?
User choose wall (just click on same wall from list) then name of this wall should be on layout until user log out or change wall. I see that .net use for this @inject but i think that i need also some global variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.