Shaver circuit breaker housing according to parameters

I am implementing a tab in mvc4. Therefore, I implemented a partial view, which, in turn, caused a tab structure in each view. The code below sets the active tab based on the parameter passed from the view.

<ul class="nav nav-tabs"> @{ var UserFocus = ""; var CardFocus = ""; var CarrierFocus = ""; } @{ switch(ViewData["ActiveMenu"].ToString()) { case "User": UserFocus = "active"; break; case "Card": CardFocus = "active"; break; case "Carrier": CarrierFocus = "active"; break; } } <li class="@UserFocus">User view link</li> <li class="@CardFocus">card view link</li> <li class="@CarrierFocus">Carrier view link</li> </ul> 

And in each performance, it will be called so on the basis of performance.

 @Html.Partial("_AdminSettings", new ViewDataDictionary {{ "ActiveMenu", "User" }} ) 

It works great.

  • I'm sure implementation is standard practice?
  • Any other easy way for this implementation?
+4
source share
1 answer

Personally, I don't like the logic in the view. As a rule, I usually put the logic in the model and pass it into the view and believe that the view should show how the data should look, and not manipulate the data.

As an alternative approach, you can have a Navigation Model and pass:

 ViewData["ActiveMenu"] 

to your controller and fill in the Navigation Model .

 public class NavigationModel { public string UserFocus { get; private set; } public string CardFocus { get; private set; } public string CarrierFocus { get; private set; } public NavigationModel(string ActiveMenu) { // Your switch statement to populate the properties. } } 

Your opinion will look like this:

 @Model NavigationModel <ul class="nav nav-tabs"> <li class="@Model.UserFocus">User view link</li> <li class="@Model.CardFocus">card view link</li> <li class="@Model.CarrierFocus">Carrier view link</li> </ul> 

Which, in my opinion, is cleaner.

+4
source

All Articles