❔ stockticker prints good but on the website viewbag.stockticker is nil, why is that?
38 Replies
Why are you using
ViewBag
and not typed models?Simplier
but holdon could it be my javascript
like the redirection?
if not ima try the strong typed models
Well, your view won't update just because you set some variable in the backend, if that's what you're trying to do
If it's rendered server-side, like a view, it's rendered, sent to the browser and that's it
To update the data you would have to reload the page
And make sure whatever new data you want is added during that request
Or you can use JS to avoid the reload
I am using js
I don't understand much but a model would fix this?
But also render things server-side?
I thought the server would send it to the view
It would make more proper code, but no, it's unlikely to solve the issue
oh
Your problem stems from the fundamental misunderstanding how websites work, what's done on the server, what's done on the client, and so on
So I can use js to get the viewbag??
No
JS has no concept of a viewbag
ik
but like a get request
to the controller
Sure, that you can do
How would you do it
I think my code is poorly structured as I have not used one query 💀
You would have an API controller
something like this
That you would then call with JS
Is there something I can use to store data across controllers and not use database
A file, if you really want
But a database would be ideal
like literally build a mongodb for this stuff?
or would cookies do
You don't need to build anything
And cookies are client-side
You can use SQLite with EF Core
Or LiteDB
also what does return RedirectToAction("Data", "Stock"); do in the controller
It redirects to another action
so it'd call the IActionResult Data()?
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.redirecttoaction?view=aspnetcore-7.0
Why?
Why do you even need to redirect in the first place?
Chatgpt said to do it to pass the viewbag
Well, ChatGPT is very often confidently incorrect
So
so if somehow I did a post request from the browser to the server right and the controller returns a view the browser view will change?
If you do it with a form or a link, yes
If you do it with
fetch
, XHR
or ajax
, nolol holdon so the viewbag goes where then
Viewbag is, tentatively, used to pass data between the controller and the view
And, sometimes, between views and layouts
and models can be passed to js?
Yes, they can be returned from a controller instead of a view
cause ig it's not dynamic
ohh
Also when is it appropiate to have like this on your website
Uh, when you want to?
You can make a complete website that never changes the address bar
All navigation is done with JS
A so-called SPA, or Single Page Application
But you sometimes want people to, dunno, send eachother links to specific videos on youtube
Or to a privacy policy
Or something
So... then
mb i meant like the {id}
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.