0

Let's say I have a field called "FooName" and I have "FooName" in several places across my web application (ASP.NET MVC4). I have a check box that allows me to turn "FooName" on and off across the application. Currently, I have access to the checked value (I can grab the saved value from the database) but I'm not sure how to show/hide "FooName" text display across the application?

Is there a utility class I can use or make? Should I use a regex to parse all my web pages? What should I do?

2

I think you are misunderstanding the MVC pattern a bit.

One of the purposes of MVC pattern is to divide your application into "layers".

  • The Model-layer which contains "data"
  • The View-layer which represents the "data" (the Model)
  • The Controller-layer which handles user input and modifies "data" (the Model)

The Model does not tell the View how the data should be presented.
The View interpretes the Model and presents the data.

This means that the Model may contain information about how data the may be presented by using properties or fields. But ultimately it's up to the View.

In your case (for example) you may add the property – Model.DisplayFooName. But this in itself will not change how your View looks because the View has not been altered. Your Model has a shiny new property that your View may use to change the way the data in your Model is presented. In essence the Model is powerless when it comes to the View. Your View decides how your Model-data should be presented, and you cannot make changes only to your Model, to change the way the data is presented in the View.

TL;DR:
Ultimately, changes to your Model cannot change how View decides to present data in it.


To tackle the actual problem:

If you have a global setting to toggle the display of your FooName, you can create a HtmlHelper to speed up the process of toggling it. Instead of having to write something like...:

if (GlobalSettings.DisplayFooName)
{
    <strong>@Model.FooName</strong>
}

... every time you want to toggle it,
you can create helper that allows you to just simply write

@Html.PrintFooName(Model.FooName)

Or even..:

@Html.PrintFooName()

...since the HtmlHelper contains the model-data.

This could be sped up by using Find & Replace.

  • Sorry for the delayed response but if I have @Html.PrintFooName(), how do I toggle it on and off then? If writing the above, prints the name and not display it at all, doesn't display it at all? – Kala J Aug 9 '15 at 23:59
  • I provided a link in my answer (see HtmlHelper). You decide what you want your helper to print/not print. :) – die maus Aug 12 '15 at 5:38
  • Instead of htmlhelper, can I use a viewmodel with bool property to show or hide "FooName"? What are the limitations of this approach instead? – Kala J Aug 18 '15 at 19:16
1

You want to have a single class that knows about your FooName current display settings. Your Views should decide to show or not the FooName based on visibility settings provided by your class.

  1. When you build your pages, pass the current true/false visibility state of your FooName(produced by that class) into viewBag,
  2. Everywhere where you have your FooName rendered have a condition that checks the boolean.
  • so basically, I have to go to to all my classes that contain FooName and pass FooName into view with conditions. Then go to all my views and change visibility for FooName. That doesn't sound friendly. It's sound a bit redundant. – Kala J Jul 23 '15 at 23:34
  • No, you need new class called FooName or something similar. Have a property FooName.Enabled. Then based on that property you either display it or not. YES, you will have to refactor your code in either case, but at least have the logic to handle FooName related calculations in one place - the class. – Alexus Jul 23 '15 at 23:42
  • Ahh I see. That makes more sense. Okay, I'll give it a try. Thanks. – Kala J Jul 23 '15 at 23:54

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.