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.
tokenhas a value? Have you debugged the controller method to see if it does?@pagedeclared? 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 aViews/PopUpAuth/Index.cshtmlto render your view. Or you just use Razor pages and put your logic in theIndexModeland trash your controller. Not bothXxxModelclass or MVC with Controller + corresponding view.