-1

I would like to know what is the best way to send a parameter to a view. This is my first time with MVC. I have a menu and I would like to change the item menu class depending which item menu is selected. For example I have a menu with Home, Products, About.

<div class="nav-main-item">
<a asp-controller="Home" asp-action="About">
            <div class="top-solid-line selected"></div>
            <div class="row nav-item">
                <div class="item-line-1">
                    Home
                </div>
            </div>
            </a>
</div>
<div class="nav-main-item">
<a asp-controller="Home" asp-action="Products">
            <div class="top-solid-line"></div>
            <div class="row nav-item">
                <div class="item-line-1">
                    Products
                </div>
            </div>
            </a>
</div>
<div class="nav-main-item">
 <a asp-controller="Home" asp-action="About">
            <div class="top-solid-line"></div>
            <div class="row nav-item">
                <div class="item-line-1">
                    About
                </div>
            </div>
            </a>
</div>

If I select Products I want to add the class 'selected' to it, and remove the class selected to the item menu was selected before an so on. I know how to do it in jquery, is very simple, but the thing is when I click one of the items menu the selected class continue in the home item menu, that is because I initialize the selected on the Home item menu. I need some kind of parameter to pass to the view and depending on that value add the class selected to the item menu selected. I know should be very simple, any help will be appreciate it! Thanks.

user2112420
  • 955
  • 5
  • 11
  • 26

2 Answers2

1

In the controller you can use return View(model), where model is some variable or object. In the cshtml file you can then declare @Model int for instance, when passing an integer and access it like a normal variable as model. For instance: @if (model == 1){ your code here }.

EDIT: you can also add parameters to the ViewBag (google it, there are lots of examples) but I would use the model since it is strongly typed.

SubliemeSiem
  • 1,129
  • 17
  • 25
  • But do you think is necessary a model for just a parameter, If I would like to return more than one model to my view, could I do that? For example if I want to send a list to the view and also a variable as you wrote me.... – user2112420 Jan 08 '16 at 12:08
  • As far as I know, you can only pass one model, but you can create a type that incorporates your model with other vars and pass that as a model instead. Otherwise you have to use the Viewbag (and for a simple parameter this might even be better than creating complex types) – SubliemeSiem Jan 08 '16 at 12:12
  • No problem, glad I could help – SubliemeSiem Jan 08 '16 at 12:15
  • If you want to use two or more models in the same view, you could see this link: http://stackoverflow.com/questions/30925122/mvc-multiple-models-in-one-view. – Jose Luis Jan 08 '16 at 12:23
  • Which is exactly what I already described; combining all models and parameters into a single model. – SubliemeSiem Jan 08 '16 at 12:25
0

ASP.NET MVC Send Multiple Data to View

My opinion

ViewBag, ViewData,TempData is basic solution method exm: (alert, message, script vb. other run client-side code and single data) but write a long code line this dirty and complexible. But using ViewModel property is best solve so your code is not complexible and full compatible Object Oriented Programming architecture.

Understanding ViewModel

rootturk
  • 316
  • 4
  • 20
  • ViewModel is not the best solve if it forces you to create more complex types. For simple parameters Viewbags are way more readable. – SubliemeSiem Jan 08 '16 at 12:27
  • Please you try read to my message and think. I know how to use field for Viewbag,ViewData,TempData vb. object. I'm recommended solution is ViewModel so Clean and availability. But The simple problem of course using the ViewModel,ViewBag, Tempdata. – rootturk Jan 08 '16 at 13:13