1

I am getting a Null reference Exception when I am trying to access from my View to a Model property.

My View is:

@page
@model WebApp.Pages.IndexModel
<p id="token" style="visibility: hidden;">@Model.Token</p>

My Model is:

public class IndexModel : PageModel
{
    private string token = "token";

    public string Token{ get { return token; } }

    public IndexModel(string token)
    {
        this.token = token;
    }
}

And my Controller is:

public class PopUpAuthController : Controller
{
    public PopUpAuthController()
    {
    }

    public IActionResult Index()
    {
        return View(new IndexModel("TokenPlaceholder"));
    }
}

The @Model.Token is throwing me the exception. I tried to use DataView["Token"] for setting and getting but it throws the same exception.

I have very very simple code but I cant get with this error. I hope you can help me folks.

theduck
  • 2,589
  • 13
  • 17
  • 23
Mr.Deer
  • 476
  • 6
  • 17
  • 2
    Are you sure `token` has a value? Have you debugged the controller method to see if it does? – JamesS Aug 16 '19 at 09:17
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Owen Pauling Aug 16 '19 at 09:18
  • Yes, I debugged it and it has a value at the moment of calling the View constructor.. It very strange – Mr.Deer Aug 16 '19 at 09:21
  • @OwenPauling I dont thing is a duplicate. I know how to solve normally the null exceptions. The problem is that I think there is something wrong in my code regarding to the mvc. – Mr.Deer Aug 16 '19 at 09:22
  • Add the _set{Token = value;}_ to your IndexModel.Token. – Steve Aug 16 '19 at 09:22
  • Not sure if typo in code or just SO but - "stlye" – andyb952 Aug 16 '19 at 09:23
  • @andyb952 Thank you it was a mistake that :) But still the null exception. – Mr.Deer Aug 16 '19 at 09:24
  • @Steve Is this really neccesary here? I mont sure about that. – Mr.Deer Aug 16 '19 at 09:25
  • 4
    I am confused. You use a controller but have `@page` declared? What are you using now? MVC or Razor pages (which don't use controllers)? Razor pages are indepdendent of controllers and (unlike the name suggests) not (data transfer or view) models. When using a controller and an action named "Index" you need a `Views/PopUpAuth/Index.cshtml` to render your view. Or you just use Razor pages and put your logic in the `IndexModel` and trash your controller. Not both – Tseng Aug 16 '19 at 09:35
  • 1
    You have to decide for one technology (Razor or MVC) and use that. You can mix both in the same application (iirc), but either RazorPages + Loginc inside `XxxModel` class or MVC with Controller + corresponding view. – Tseng Aug 16 '19 at 09:41
  • Add a debug comment into your page: @if(Model == null) {

    null model

    } else { // your code }}
    – fireydude Aug 16 '19 at 09:45
  • @Tseng I deleted the @ page and now is it working well. Thank you so much! – Mr.Deer Aug 16 '19 at 10:17
  • Well if you just delete the `@page` its only the half way. Your `IndexModel` needs to be replaced with a proper model too, which is a POCO (and not inheriting from `PageModel`) – Tseng Aug 16 '19 at 11:11

1 Answers1

2

Seems you are mixing razor pages (with @page declaration) and MVC here.

You should use only one per page, either regular cshtml Razor view (placed in folder named Views/PopUpAuth/Index.cshtml) and use a regular POCO model (no inheritance from PageModel).

Alternatively you just use razor view and put in your logic in the PopUpAuthModel's OnGet method and delete your controller.

You can use both in the same project, but you can't mix them on the same endpoint/route/page.

Tseng
  • 61,549
  • 15
  • 193
  • 205