Guilherme T.I.
Guilherme T.I.

Reputation: 35

Null reference when using model value in the view

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?

Upvotes: 0

Views: 160

Answers (2)

thangchung
thangchung

Reputation: 2030

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

Upvotes: 1

technicallyjosh
technicallyjosh

Reputation: 3531

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>

Upvotes: 0

Related Questions