2
\$\begingroup\$

I have the code below and I am looking for input as to how I can improve it.

The main question here is should the code at the top in razor brackets be moved to the Controller and returned via a ViewModel?

The underlying question being, particularly when the code isnt controling styling is should the endeavour be to have as much as the C# code inside the controller as much as possilble, which i feel i am good at doing, and getting better at despite a few areas of lazyness.

@{
    var show = ViewBag.ShowBasket == "true";

    var visiblityLg = show ? "visible-lg" : "hidden hidden-lg";
    var visiblityMd = show ? "visible-md" : "hidden hidden-md";
    var visiblitySm = show ? "visible-sm" : "hidden hidden-sm";
    var visiblityXs = show ? "visible-xs" : "hidden hidden-xs";
}

<a href="@Url.Action("Index", "ShoppingBasket")" class="navbar-icon navbar-icon-cart @visiblityLg" title="Shopping Basket">
    <span class="fa fa-shopping-cart fa-4x text-warning"></span>
</a>


<a href="@Url.Action("Index", "ShoppingBasket")" class="navbar-icon navbar-icon-cart-md @visiblityMd" title="Shopping Basket">
    <span class="fa fa-shopping-cart fa-3x text-warning"></span>
</a>


<a href="@Url.Action("Index", "ShoppingBasket")" class="navbar-icon navbar-icon-cart-sm @visiblitySm" title="Shopping Basket">
    <span class="fa fa-shopping-cart fa-2x text-warning"></span>
</a>


<a href="@Url.Action("Index", "ShoppingBasket")" class="@visiblityXs" title="Shopping Basket">
    <span class="fa fa-shopping-cart text-warning"></span>&nbsp;Shopping Basket
</a>
\$\endgroup\$
  • \$\begingroup\$ Please correct the title so that is says what your code is doing and not what you are going to do with the code. \$\endgroup\$ – t3chb0t Apr 12 '17 at 8:11
  • \$\begingroup\$ As for me this code is fine. Controller evaluate what it should - show or not to show basket. And it's responsibility of View to decide how to show or not to show this basket. Maybe it will be included into generated HTML but will be hidden, maybe it will not included into HTML, maybe some special image will be inserted into page if basket should not be shown. Imho, It's totally View responsibility. \$\endgroup\$ – Disappointed Apr 12 '17 at 11:06
1
\$\begingroup\$

I would argue that the code in the top should not be moved to Controller.

This of course depends on how you define responsibilities for View, Controller and Model, but I would leave the code in the View because Controller and Model should not have knowledge about CSS if it can be avoided.

More about responsibilities in ASP.NET MVC

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.