C
C#•7mo ago
pape

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
pape
papeOP•7mo ago
And how can i fix it
Angius
Angius•7mo ago
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
pape
papeOP•7mo ago
im new to here, how do i write code here so it isnt just normal text?
Angius
Angius•7mo ago
$code
MODiX
MODiX•7mo ago
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/
pape
papeOP•7mo ago
'''cs
Angius
Angius•7mo ago
` not '
pape
papeOP•7mo ago
wait i didnt delete everything, it showed -500 chars cause i dont have nitro
Angius
Angius•7mo ago
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
pape
papeOP•7mo ago
do i just share the link of it now?
Angius
Angius•7mo ago
ye
pape
papeOP•7mo ago
BlazeBin - zsoajhvrzjka
A tool for sharing your source code with the world!
pape
papeOP•7mo ago
line 5 is the problem It reads it as null
Angius
Angius•7mo ago
Well, that's the view What's the controller action behind it?
kurumi
kurumi•7mo ago
debug controller
pape
papeOP•7mo ago
what do i have to write in the controller to make it not null?
pape
papeOP•7mo ago
BlazeBin - brysjccnzvht
A tool for sharing your source code with the world!
Angius
Angius•7mo ago
Idk, what do you have there already? None of the methods in this controller return an IndexModel You sure it's the correct one?
pape
papeOP•7mo ago
that is the most basic homecontroller
Angius
Angius•7mo ago
The view says it wants an instance of IndexModel as a... model
@model BuyCars.Views.Home.IndexModel
@model BuyCars.Views.Home.IndexModel
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 model
pape
papeOP•7mo ago
BlazeBin - imeqyljobaip
A tool for sharing your source code with the world!
pape
papeOP•7mo ago
what about this? im going insane because i dont know what i have to do
Angius
Angius•7mo ago
That is a backing code for a Razor Pages page
pape
papeOP•7mo ago
and this is mvc right
Angius
Angius•7mo ago
Razor Pages != MVC
pape
papeOP•7mo ago
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
Angius
Angius•7mo ago
It's in the name, yes Model, View, Controller
pape
papeOP•7mo ago
yeah i know 😦
Angius
Angius•7mo ago
Razor Pages kinda merge the controller and the model parts together
pape
papeOP•7mo ago
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
Angius
Angius•7mo ago
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 reason
pape
papeOP•7mo ago
yeah i know its cause im dumb and out of patience ill look at some tutorials but thank you
Angius
Angius•7mo ago
Well, if you know the mistake, alleviate it
pape
papeOP•7mo ago
wanted to check if there was a some normal quick fix instead of a hours long tutorial
Angius
Angius•7mo ago
Keep views in Views
pape
papeOP•7mo ago
ye of course
Angius
Angius•7mo ago
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?
pape
papeOP•7mo ago
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
Want results from more Discord servers?
Add your server