1

I have an mvc app that works mostly client side with knockoutjs. I have the need of configuring it for different type of installations so I will have custom parameters for some colors for example. Now I put everything in a js file on the client, but because this is something that does not change while the app is running I was thinking to put this on the server side and modify my view depending on those custom params so the page will just be rendered server side with the right colors.

What is the best way of doing this with MVC? all the answers I found use: ConfigurationManager.GetSection to read from the web.config file but it's not very clear if it works in mvc and if after that I need to put the values in a model and attach it to the view.

something like this is suggested all the time: var somevar = "@item" but what is item in an view?

Maurizio In denmark
  • 4,226
  • 2
  • 30
  • 64

4 Answers4

1

you can set a variable in a controller and use it in the view like this:

ViewBag.msg="Hello!";

and get it in the view:

=@ViewBag.msg;
Super Hornet
  • 2,839
  • 5
  • 27
  • 55
1

You can add <appSettings> section to the web.config. Add <add key="" value="" /> strings inside. Then you can use such values in your controllers like this:

ViewData["YourKey"] = ConfigurationManager.AppSettings["YourKey"].

Finally, write something similar in views:

var value = @(ViewData["YourKey"])
Nikita.D.
  • 11
  • 4
1

Expanding on the other answers, it's possible to inject this value into your clientside javascript. You'd just do something like this, if the JS is in a CSHTML file:

//Controller Action
public ActionResult SomeAction()
{
    ViewBag.Color = "green"; // Get this from ConfigurationManager.AppSettings, database, or anywhere
    return View();
}

// on the view
<script type="text/javascript">
    var myColor = '@ViewBag.Color';
    // use myColor as any other jS variable
</script>

If your javascript is in a separate .js file, you'll have to the variable from the .cshtml (or .aspx) before using it in the .js file.

To Answer your question about what item is in a view:

In this case, it's a variable on the razor page. If the view is strongly typed, it will have a model defined at the top, then Model is a variable that can be used on the page using @Model. If the model is a collection of some sort, often times people will loop through it and output an item for each item in the collection like:

@foreach(var item in Model)
{
  <span>@item</span>
}

but the variable can also be created on the fly, and used however you want, like:

@{
    var item = "some string";
    var foo = "bar";
 }

then used in your javascript, like above:

<script type="text/javascript">
    var item = '@item';
</script>
xdumaine
  • 10,096
  • 6
  • 62
  • 103
  • 1
    so what is better for customization variables, VieBag or VievData? – Maurizio In denmark Aug 27 '13 at 13:16
  • 1
    @MaurizioIndenmark Check out [this question](http://stackoverflow.com/questions/4705426/whats-the-difference-between-viewdata-and-viewbag) - They're basically the same, but ViewBag is an expando which allows you to use property notation instead of stringed key notation (i.e., "Magic strings") – xdumaine Aug 27 '13 at 13:21
1

You have some options. You can use ViewBag, ViewData or have it as a Property on the model. My preference is the model approach as it keeps the relationship between the view and the controller strict and well defined.

The @ that you are referring to is normally the razor syntax to indicate that what follows is server-side code (or code that will be executed at load time on the server). For example @Model is the model, @item is a server side variable called item. You will also use Html helpers when setting web elements that take a lambda (model=>model.Description) where Description is a property of the model.

So, you can do quite cool things like set a JavaScript variable to a value that is calculated on the server.

var temperature = 0; //javascript
temperature  = @temperatureCalculation; //@temperatureCalculation is a C# variable that holds the result of a temperature calculation that is done on the server.

There is quite a bit of info out there about ViewBags, ViewData, Models and the razor syntax.

acarlon
  • 16,764
  • 7
  • 75
  • 94
  • can temperatureCalculation be in the controller action that returns that view? – Maurizio In denmark Aug 27 '13 at 13:36
  • It can, though I have only done it where a static method was used on the controller. I guess you could have the controller as a member of the model. Though, probably best to do the calculation first in the controller, then put the result in the model/ViewBag/ViewData. Otherwise the calculation could be in the model or in the view itself. – acarlon Aug 27 '13 at 14:16