Need help with mvc,NullReferenceException: Object reference not set to an instance of an object.
NullReferenceException: Object reference not set to an instance of an object.
ViewData["Title"] = "Index";
can somebody please explain to me what this means?
38 Replies
And how can i fix it
Well, it means something was null
The fix is to make it not null
We'll need to see a bit more code to be more specific
Which controller action causes that issue, which view, etc
im new to here, how do i write code here so it isnt just normal text?
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/'''cs
` not '
wait i didnt delete everything, it showed -500 chars
cause i dont have nitro
Depending on the keyboard layout, the key below escape
In that case, as the bot message says, use some paste
pastie.io, hastebin, paste.mod.gg
Whichever
do i just share the link of it now?
ye
BlazeBin - zsoajhvrzjka
A tool for sharing your source code with the world!
line 5 is the problem
It reads it as null
Well, that's the view
What's the controller action behind it?
debug controller
what do i have to write in the controller to make it not null?
BlazeBin - brysjccnzvht
A tool for sharing your source code with the world!
Idk, what do you have there already?
None of the methods in this controller return an
IndexModel
You sure it's the correct one?that is the most basic homecontroller
The view says it wants an instance of
IndexModel
as a... model
None of the methods in your controller send any sort of data to the view
Well, the Error
action passes an ErrorViewModel
But it's neither the action that seems to be rendering this view, nor is it the desired modelBlazeBin - imeqyljobaip
A tool for sharing your source code with the world!
what about this?
im going insane
because i dont know what i have to do
That is a backing code for a Razor Pages page
and this is mvc right
Razor Pages != MVC
cause i already did work for razer, and i know that they are different
but i just dont know how to do mvc, razer is 1000 times easier for me
do i have to make seperate model
for every page
and controller
It's in the name, yes
Model, View, Controller
yeah i know 😦
Razor Pages kinda merge the controller and the model parts together
and makes it so much easier, but for my online course all of a sudden i had to make mvc instead of razer and it kinda fucked me over
In any case, in this Razor Pages code, your
Car
can be null
The ViewData should not be, however
Make sure that it's actually the Razor codebehind that serves this page
And not one of the MVC controllers mistaking it for a view it should render itself
It's generally best to not mix both in the same project. And if you do so, they should never be in the same folder
The way your Razor page seems to be in the... views folder
For some godforsaken reasonyeah i know
its cause im dumb
and out of patience
ill look at some tutorials but thank you
Well, if you know the mistake, alleviate it
wanted to check if there was a some normal quick fix instead of a hours long tutorial
Keep views in
Views
ye of course
Controllers in
Controllers
Pages in Pages
And a fix always depends on the circumstances
So my best tip would be to use the debugger or logging to see where the code goes
What it executes
Is it a controller trying to render this template? A page?
What is null and when?Basically i think its the controller
and the view page is in razer
thats the main problem
ill watch a tutorial and do step by step
with mvc