0

I am declaring my values in web.config

 <appSettings>
    <add key="SystemName" value="RealState Premium" />
    <add key="SystemDescription" value="Sistema de Administração Imobiliário" />
  </appSettings>

Configured a model:

    public class _Header
    {
        public string SystemName { get; set; }
        public string SystemDescription { get; set; }
    }

Controller:

    [AllowAnonymous]
    public ActionResult _Header()
    {
        HomeModels._Header headerModel = new HomeModels._Header();

        headerModel.SystemName = ConfigurationManager.AppSettings["SystemName"];
        headerModel.SystemDescription = ConfigurationManager.AppSettings["SystemDescription"];

        return PartialView(headerModel);
    }

and for the last, View:

@using realstate.Models
@model HomeModels._Header

<div class="logo">
    <img src="~/Images/logo/logo.png" alt="Mercado de Imóveis" />
    <table class="sysTitleTbl">
        <tr>
            <td class="name">@Model.SystemName</td>
        </tr>
        <tr>
            <td class="description">@Model.SystemDescription</td>
        </tr>
    </table>
</div>

The problem is that I am getting a null reference in @Model.SystemName What would be the problem?

  • What is the location of the Web.Config you placed the appSettings into relative to the root of your project? They must be placed into the web.config at the root; if you placed them into a config inside a Views folder, they will be irrelevant. – Moby's Stunt Double May 23 '13 at 21:15
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 24 '13 at 06:30

2 Answers2

1

Set break point to these line of code

headerModel.SystemName = ConfigurationManager.AppSettings["SystemName"];
headerModel.SystemDescription = ConfigurationManager.AppSettings["SystemDescription"];

to see what' s happening with it

thangchung
  • 2,020
  • 2
  • 17
  • 28
0

Make sure that you are setting the appSettings node in the "/web.config" file and not the "views/web.config" file. Also, make sure that it sits within "configuration".

Example:

<configuration>
    <appSettings>
        <add key="SystemName" value="RealState Premium" />
        <add key="SystemDescription" value="Sistema de Administração Imobiliário" />
    </appSettings>
</configuration>
technicallyjosh
  • 3,511
  • 15
  • 17